L1-039 古风排版 (20分)【字符串处理】

原题链接
思路:修正字符串 扫描字符串 按对四取余为下标分类 开数组存储 倒着输出就ok

#include <bits/stdc++.h>

using namespace std;

string str;
int main()
{
    
    
    int n;
    cin >> n;
    getchar();
    string str;
    getline(cin, str);
    int flen = str.length(); //初始字符串长度
    int lie;
    if(flen % n == 0) lie = flen / n;
    else
    {
    
    
        lie = (flen / n) + 1;
        while(str.length() < lie * n)
        {
    
    
            str +=" ";//修正字符串
        }
    }
    int len = str.length();
    vector<vector<char>> a(n + 1);
    for(int i = 0; i < len; i++)
    {
    
    
        a[i % n].push_back(str[i]);
    }
    for(int i = 0; i < n; i++)
    {
    
    
        int s = a[i].size();
        for(int j = s - 1; j >= 0; j--)
        {
    
    
            cout << a[i][j];
        }
        cout << endl;
    }
}

猜你喜欢

转载自blog.csdn.net/moumoumouwang/article/details/109269862