版权声明:本文为博主原创文章,未经博主允许不得转载,欢迎添加友链。 https://blog.csdn.net/qq_42835910/article/details/88606180
输入正整数n以及n个文件名,排序后按列优先的方式左对齐输出。假设最长文件名
有M字符,则最右列有M字符,其他列都是M+2字符。
样例输入(略,可以由样例输出推出)
样例输出:
【分析】
首先计算出M并算出行数,然后逐行逐列输出。
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100+5, maxL = 60;
string file[N];
void print(const string& s,int len){
cout<< s;
for(int i = s.length(); i < len; i++) cout<< " ";
}
int main(int argc, char** argv) {
int n;
while(cin>> n){
int maxLength = 0;
for(int i = 0; i < n; i++){
cin>> file[i];
maxLength = max(maxLength, (int)file[i].length());
}
sort(file, file+n);
int column = (maxL - maxLength)/(maxLength+2)+1, row = (n-1)/column+1;
for(int i = 0; i < 60; i++) cout<< "-";
cout<< "\n";
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
int idx = j*row+i;
if(idx < n)print(file[idx],j == column-1 ? maxLength : maxLength+2);
}
cout<< "\n";
}
}
return 0;
}