服了,最后一行str读完了,必须要用空格填充
C++ 代码
#include<iostream>
#include<string>
using namespace std;
const int N = 1010;
int n;
string str;
char c[N][N];
int main()
{
cin >> n;
getchar(); //读入回车
getline(cin, str);
int m = str.size();
int w;
int k = 0;
if (m % n == 0)w = m / n;
else w = m / n + 1;
for (int i = w; i >= 1; i--) {
for (int j = 1; j <= n; j++) {
if (k < m) {
c[j][i] = str[k++];
} else {
c[j][i] = ' '; // 如果str已读完,用空格填充
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= w; j++)
{
cout << c[i][j];
}
cout << endl;
}
return 0;
}