西西里1007

放了20多天假,一直都没有怎么打码,学渣都快变成学沫了……不行,要振作起来!


解码方法:

1


解题过程中遇到的问题:

  1. s.size()的返回值是否包含 ‘\0’?(不包含)
  2. 求矩阵行数时直接用字符串长度除以3(列数是个变量), 导致输入其他测例时出错。

代码:

#include <iostream>
#include <string>

using namespace std;

int main() {
    while(1) {
        int col = 0;
        cin >> col;
        if(col == 0) return 0;

        string s;
        cin >> s;

        int row = s.size() / col;

        char matrix[row][col + 1];

        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                matrix[i][j] = s[i * col + j];
            }
        }

        for(int i = 0; i < row; i++) {
            if(i % 2 == 0) continue;
            string temp;
            for(int j = col - 1; j >= 0; j--) {
                temp += matrix[i][j];
            }
            for(int j = 0; j < col; j++) {
                matrix[i][j] = temp[j];
            }
        }


        for(int i = 0; i < col; i++) {
            for(int j = 0; j < row; j++) {
                cout << matrix[j][i];
            }
        }

        cout << '\n';
    }
}

猜你喜欢

转载自blog.csdn.net/cat_xing/article/details/76383206