我是靠谱客的博主 寒冷唇膏,最近开发中收集的这篇文章主要介绍UVa 400 - Unix ls解题报告,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一道简单的字符串排序问题,只是输出格式要注意。找到规律就不难了。


//400 - Unix ls
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int cmp_words(const void *_a, const void *_b)
{
	char *a = (char*)_a;
	char *b = (char*)_b;
	return strcmp(a, b);
}
char words[110][100];
void space(int, int);
int main()
{
	//freopen("data.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n;
	while (scanf("%d", &n) != EOF)
	{
		memset(words, 0, sizeof(words));
		getchar();
		for(int i = 0; i < n; i++)
			scanf("%s", words[i]);

		qsort(words, n, sizeof(words[0]), cmp_words);//排序

		int maxlen = strlen(words[0]);
		for(int i = 1; i < n; i++)//找到最大长度
			if(maxlen < strlen(words[i]))
				maxlen = strlen(words[i]);

		int col = 62 / (maxlen + 2);//这里之前判断条件写错,有漏洞,导致wrong。
		int row = (n - 1) / col + 1;
		for(int i = 0; i < 60; i++)
			printf("-");
		printf("n");
		for(int i = 0; i < row; i++)//按要求输出
		{
			for(int j = 0; j < col && (i + row * j) < n; j++)
			{
				
				printf("%s", words[i + row * j]);
				/*if(j != col - 1)*/
					space(strlen(words[i + row * j]), maxlen);//这里最后一列的空格可以输出,不影响结果。
				/*else
					space(strlen(words[i + row * j]) + 2, maxlen);*/
			}
			printf("n");
		}
	}
	return 0;
}

void space(int len, int maxlen)
{
	int n = maxlen + 2 - len;
	for(int i = 0; i < n; i++)
		printf(" ");
}


最后

以上就是寒冷唇膏为你收集整理的UVa 400 - Unix ls解题报告的全部内容,希望文章能够帮你解决UVa 400 - Unix ls解题报告所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部