当前位置:网站首页>Openjudge noi 1.13 17: text layout

Openjudge noi 1.13 17: text layout

2022-06-11 02:49:00 Junyi_ noip

【 Topic link 】

OpenJudge NOI 1.13 17: Typesetting

【 Topic test site 】

1. character string

【 Their thinking 】

solution 1: Save the number of characters that a line has occupied

set up h Is the number of characters occupied by the current line .
Add the first word directly , Include the space in front of the word when adding the word , So every time you add a word , The occupied characters are increased : Word length +1.

  • If h by 0, Output the word ,h Just increase the length of one word .
  • If h Add the current word length +1 after , Greater than 80 Characters , Then the word should be placed on the next line . Line break , Output the word , Then h Change to the current word length .
  • Other situations , Output the space and the word ,h Increase word length +1.

solution 2: Save the number of characters remaining in a line

set up r The number of characters remaining for the current line that can be added .
Add the first word directly , Include the space in front of the word when adding the word , So every time you add a word , The number of remaining characters is reduced : Word length +1.

  • If r by 80, Output a word ,r Reduce the length of only one word .
  • If r Subtract the current word length +1 After less than 0, Then the word should be placed on the next line . Line break , Output the word , Then r Turn into 80 Subtract the current word length .
  • Other situations , Output the space and the word ,r Reduce : Word length +1.

You can output while traversing . You can also construct a string first , The final output .

【 Solution code 】

solution 1: Save the number of characters that a line has occupied + Construct string

#include<bits/stdc++.h>
using namespace std;
#define L 80 // One line length 
int main()
{
    
    string s[1005], ans;
    int n, h = 0;//h: Number of existing characters 
    cin >> n;
    for(int i = 1; i <= n; ++i)
        cin >> s[i];
    for(int i = 1; i <= n; ++i)
    {
    
        if(h == 0)
        {
    
            ans = ans + s[i];
            h += s[i].length();
        }
        else if(h+s[i].length()+1 <= L)
        {
    
            ans = ans + ' ' + s[i];
            h += s[i].length()+1;
        }
        else//h+s.length()+1 > L 
        {
    
            ans = ans + '\n' + s[i];
            h = s[i].length();
        }
    }
    cout << ans;
    return 0;
}

solution 2: Save the number of characters remaining in a line + Direct output

#include<bits/stdc++.h>
using namespace std;
#define L 80 // One line length 
int main()
{
    
    string s;
    int n, r = L;//r: Number of characters remaining 
    cin >> n;
    for(int i = 1; i <= n; ++i)
    {
    
        cin >> s;
        if(r == L)
        {
    
            cout << s;
            r -= s.length();
        }
        else if(r >= s.length()+1)
        {
    
            cout << ' ' << s;
            r -= s.length()+1;
        }
        else// r < s.length()+1 
        {
    
            cout << endl << s;
            r = L - s.length();
        }
    }
    return 0;
}
原网站

版权声明
本文为[Junyi_ noip]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110204013447.html