注意如果矩阵元素为空(null,与空格区分),输出空格
#include <iostream>
using namespace std;
const int N = 1e3 + 10;
char g[N][N];
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n; cin >> n; cin.get();
string s; getline(cin, s);
int len = (int)s.size();
int col = len / n + (len % n != 0);
int idx = 0;
for (int i = col; i; -- i) {
for (int j = 1; j <= n; ++ j) {
if (idx <= len - 1) g[j][i] = s[idx ++ ];
}
}
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= col; ++ j) {
if (g[i][j]) cout << g[i][j];
else cout << ' ' ;
}
cout << endl;
}
}