UVa 10905 - Children's Game

传送门UVa 10905 - Children's Game


参考了GooMaple的解题报告。

一开始也想用比较字符串的方法,但是只想到了单个进行比较。但是遇到9和90的话应该是990,按照我的想法就变成909了。。


应该比较两个连起来的字符串。。。


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;

int cmp(const void *_a, const void *_b)
{
	char *a = (char *)_a;
	char *b = (char *)_b;
	char str[1000], sstr[1000];
	sprintf(str, "%s%s", a, b);
	sprintf(sstr, "%s%s", b, a);
	return strcmp(sstr, str);
}

char num[100][100];

int main()
{
	//freopen("input.txt", "r", stdin);
	int n, i, j;
	while (scanf("%d%*c", &n), n)
	{
		for (i = 0; i < n; i++)
			scanf("%s", num[i]);
		qsort(num, n, sizeof(num[0]), cmp);
		for (i = 0; i < n; i++)
			printf("%s", num[i]);
		printf("\n");
	}
	return 0;
}



你可能感兴趣的:(ACM,uva)