题目描述
输入格式
输出格式
输入
10
tiny
2short4me
very_long_file_name
shorter
size-1
size2
size3
much_longer_name
12345678.123
mid_size_name
12
Weaser
Alfalfa
Stimey
Buckwheat
Porky
Joe
Darla
Cotton
Butch
Froggy
Mrs_Crabapple
P.D.
19
Mr._French
Jody
Buffy
Sissy
Keith
Danny
Lori
Chris
Shirley
Marsha
Jan
Cindy
Carol
Mike
Greg
Peter
Bobby
Alice
Ruben
输出
------------------------------------------------------------
12345678.123 size-1
2short4me size2
mid_size_name size3
much_longer_name tiny
shorter very_long_file_name
------------------------------------------------------------
Alfalfa Cotton Joe Porky
Buckwheat Darla Mrs_Crabapple Stimey
Butch Froggy P.D. Weaser
------------------------------------------------------------
Alice Chris Jan Marsha Ruben
Bobby Cindy Jody Mike Shirley
Buffy Danny Keith Mr._French Sissy
Carol Greg Lori Peter
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100+5;
const int maxcol = 60;
string filenames[maxn];
void print(string& str,int len,char op){
cout<<str;
for( int i=0; i<len-str.length(); i++ ) cout<<op;
}
int main()
{
int n;
while(scanf("%d",&n)==1&&n){
int M=0;
for( int i=0; i<n; i++ ){
cin>>filenames[i];M=max(M,(int)filenames[i].length());
}
sort(filenames,filenames+n);
printf("------------------------------------------------------------\n");
int col=(maxcol-M)/(M+2)+1,row=(n-1)/col+1;
for( int i=0; i<row; i++ ){
for( int j=0; j<col; j++ ){
int ind=j*row+i;
if(ind<n) print(filenames[ind],M,' ');
}
printf("\n");
}
}
return 0;
}
来源:https://blog.csdn.net/weixin_43323172/article/details/99461308