uva 10905 Children's Game

贪婪法,先排序。数字好像超过了一般整数的大小,数组稍微开大点。

 

 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char num[55][200];

int cmp(const void *a, const void* b) {
	char *aa = (char*) a;
	char *bb = (char*) b;
	char m[400] = { 0 };
	char n[400] = { 0 };
	strcat(m, aa);
	strcat(m, bb);
	strcat(n, bb);
	strcat(n, aa);
	return strcmp(n, m);
}

int main() {
	int n;
	while (scanf("%d", &n)) {
		if (n == 0)
			break;

		int i;
		for (i = 0; i < n; i++)
			scanf("%s", num[i]);

		qsort(num, n, 200 * sizeof(char), cmp);

		for (i = 0; i < n; i++) {
			printf("%s", num[i]);
			if (i == n - 1)
				printf("\n");
		}

	}

	return 0;
}

 

你可能感兴趣的:(uva 10905 Children's Game)