您的位置:首页 > 其它

UVA 400 Unix的Is命令

2012-09-01 16:28 260 查看
不喜欢这种输出格式的题。。

先读入所有的词汇,按照字典顺序排序

每行60个字符,采用最少的行数,每行最大列数为最大字符长度+2,最后一个字符不算在内

参考:http://www.cppblog.com/rakerichard/archive/2011/04/09/143808.html

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <deque>
using namespace std;
///
#define INF 0xffffff7
#define MAXN 200
///
string r[MAXN], ls[MAXN][MAXN];
int max(int a, int b)
{
if (a > b)
return a;
else
return b;
}

int main()
{
///
int i, j;
int n;
while (cin >> n)
{
int max_length(0);
for (i = 1; i <= n; i++)
{
cin >> r[i];
max_length = max(max_length, static_cast<int>(r[i].size()));
}
sort(r + 1, r + n + 1);
int row_num, column_num;
for (row_num = 1; row_num <= n; row_num++)
{
column_num = n / row_num;
if (n % row_num)
column_num++;
if( (max_length + 2) * (column_num - 1) + max_length <= 60)
break;
}
int x, y, k;
for (i = 1, x = 1, y = 1; i <= n; i++)
{
ls[x][y] = r[i];
x++;
if (x > row_num)
{
x = 1;
y++;
}
}
for(i = 1; i <= 60; i++)
cout<<"-";
cout << endl;
for (i = 1; i <= row_num; i++)
{
for (j = 1; j <= column_num; j++)
{
if ((j - 1) * row_num + i <= n)
{
cout << ls[i][j];
for(int k = ls[i][j].size(); k < (j == column_num? max_length:(max_length+2)); k++)
cout<<" ";
}
}
cout<<endl;
}

}

///
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: