每行最多60个字符
在尽量少的行中将字符串按字典序向下输出
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 100;
struct ceil{
char s[maxn];
int len;
};
struct ceil p[maxn+50], temp;
bool cmp( struct ceil x, struct ceil y )
{
if( strcmp( x.s, y.s ) > 0 ) return x.s > y.s;
}
int main()
{
int n, i, j;
int line, row;
while( ~scanf("%d",&n) ){
int M = -1;
for( i = 0; i < n; i++ ){
scanf("%s",p[i].s);
p[i].len = strlen(p[i].s);
M = p[i].len > M ? p[i].len : M;
}
line = 0;
int M2 = M + 2;
while( M + M2 <= 60 ){
line++;
M += M2;
}
line++;
row = n / line;
if( row*line < n )
row++;
int last = row*line - n;
sort(p,p+n,cmp);
puts("------------------------------------------------------------");
for( i = 0; i < row; i++ ){
int num = 0;
for( j = 0; j < line; j++ ){
if( n == 0 ) break;
if( last > 0 && i > row - last - 1 && j == line-1 ) continue;
printf("%s",p[i+num*row].s);
for(int k = 0; k < M2 - 2 - p[i+num*row].len; k++ )
printf(" ");
if(j != line-1 && n != 1 ){
if( last > 0 ){
if( i > row - last - 1){
if(j != line-2 )
printf(" ");}
else printf(" ");
}
else printf(" ");
}
num++;
n--;
}
puts("");
}
}
return 0;
}