挺有趣的模拟。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#define isokey(t) (isdigit(t) || isalpha(t) || ((t)=='.') || ((t)=='_') || ((t)=='-'))
#define max(a,b) (((a) > (b)) ? (a) : (b))
using namespace std;
const int MAXN = 100;
const int MAXL = 60;
struct FileName
{
int id;
int len;
char *s;
FileName(int id=0, int len=0, char *s=NULL)
{
this->id = id;
this->len = len;
this->s = s;
}
};
int readchar()
{
int t;
while ((t=getchar()) != EOF)
{
if (isokey(t))
{
return t;
}
if (t == '\n')
{
return -1;
}
}
}
int n;
FileName filename[MAXN+2];
char tempname[MAXN+2][MAXL+2];
void read_filename()
{
memset(filename, 0, sizeof(filename));
memset(tempname, 0, sizeof(tempname));
for (int i = 1; i <= n; i++)
{
int l = 0;
int ch;
while (ch = readchar())
{
if (ch == -1)
{
break;
}
tempname[i][l++] = ch;
}
tempname[i][l] = '\0';
filename[i] = FileName(i, l, tempname[i]);
}
}
bool _cmp(FileName a, FileName b)
{
return (strcmp(a.s, b.s) == -1);
}
int find_max()
{
int max_len = 0;
for (int i = 1; i <= n; i++)
{
max_len = max(max_len, filename[i].len);
}
return max_len;
}
void _print()
{
for (int i = 1; i <= MAXL; i++)
{
putchar('-');
}
putchar('\n');
int max_len = find_max();
int col = (MAXL+2) / (max_len+2);
int row = (n%col == 0) ? (n/col) : (n/col + 1);
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
int id = (j-1)*row + i;
if (id <= n)
{
printf("%-*s", max_len + ((j==col)? 0 : 2), filename[id].s);
}
}
putchar('\n');
}
}
int main()
{
while (scanf("%d", &n) != EOF)
{
getchar();
read_filename();
sort(filename+1, filename+n+1, _cmp);
_print();
}
}