一开始没怎么看懂题目,原来就是M字符就是这一列的宽度为M个字符,包括空格。
1 #include<iostream> 2 #include<algorithm> 3 #include<string> 4 using namespace std; 5 6 int main() 7 { 8 string filename[110]; 9 int n; 10 while (cin >> n) 11 { 12 int cols, rows; 13 int M = 0; 14 for (int i = 0; i < n; i++) 15 { 16 cin >> filename[i]; 17 M = max(M, (int)filename[i].length()); 18 } 19 cols = (60 - M) / (M + 2) + 1; 20 rows = (n - 1) / cols + 1; 21 sort(filename, filename + n); 22 for (int i = 1; i <= 60; i++) 23 cout << "-"; 24 cout << endl; 25 for (int i = 0; i < rows; i++) 26 { 27 for (int j = 0; j < cols; j++) 28 { 29 int t = j * rows + i; 30 if (t>=n) 31 continue; 32 cout << filename[t]; 33 for (int k = 1; k <= M - filename[t].length();k++) 34 { 35 cout << " "; 36 } 37 if (j < cols - 1) 38 cout << " "; 39 } 40 cout << endl; 41 } 42 } 43 return 0; 44 }