UVA 400 Unix的Is命令

不喜欢这种输出格式的题。。

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

每行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;

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值