这是一道模拟题,首先字典序排序,然后每列的长度是最长字符串的长度加二,每列不超过60个字符,注意最长字符串大于58的情况。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define mem(a) memset(a, 0, sizeof(a))
using namespace std;
struct node
{
char ch[65];
int len;
}nd[105];
bool cmp(const node & a, const node & b)
{
if(strcmp(a.ch, b.ch) > 0)
return false;
else
return true;
}
int main(int argc, char *argv[])
{
int i, j, k, n, len, maxlen, x, y, z, w;
while(scanf("%d", &n) != EOF)
{
mem(nd);
maxlen = 0;
w = 0;
for(i = 0;i < n;i++)
{
scanf("%s", nd[i].ch);
nd[i].len = strlen(nd[i].ch);
if(maxlen < nd[i].len)
maxlen = nd[i].len;
}
maxlen += 2;
if(maxlen <= 60)
y = 60 / maxlen;
else
y = 1;
z = n % y;
if(z == 0)
x = n / y;
else
x = (n + y - z) / y;
sort(nd, nd + n, cmp);
printf("------------------------------------------------------------\n");
while(w < x)
{
j = w;
while(j < n)
{
printf("%-s", nd[j].ch);
for(k = 0;k < (maxlen - nd[j].len);k++)
{
printf(" ");
}
j += x;
}
printf("\n");
w++;
}
}
return 0;
}