UVA 10905(Children's Game-C的qsort函数和sprintf)
原创
©著作权归作者所有:来自51CTO博客作者mb62d7cd05038ca的原创作品,请联系作者获取转载授权,否则将追究法律责任
4th IIUC Inter-University Programming Contest, 2005 |
A | Children’s Game |
Input: standard input Output: standard output |
Problemsetter: Md. Kamruzzaman |
给你 N 个正数. 如 123, 124, 56, 90 ,它们可以拼成 1231245690, 1241235690, 5612312490, 9012312456, 9056124123 etc. 有 24 种拼法, 9056124123 最大.
请你找出这个最大的数。
Input
每组数据第一行为 N (≤ 50). 接下来1行有 N 个正数,数据以 0 结尾.
Output
对每组数据,输出最大的拼法.
Sample Input | Output for Sample Input |
4 123 124 56 90 5 123 124 56 90 9 5 9 9 9 9 9 0 | 9056124123 99056124123 99999 |
按照字符串连起来后(len相等)的字符串比较
值得一提的是qsort中cmp的写法(char[]无法用sort排序)
以及sprintf(占位符随便改,可以将任意类型转换成字符串)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<functional>
using namespace std;
#define MAXN (100+10)
int cmp(const void *a1,const void *b1)
{
char *a=(char*)a1,*b=(char*)b1;
char _a[MAXN*2]={0},_b[MAXN*2]={0};
sprintf(_a,"%s%s",a,b);
sprintf(_b,"%s%s",b,a);
return strcmp(_b,_a);
}
int n;
char a[MAXN][MAXN];
int main()
{
while(scanf("%d",&n)&&n)
{
for (int i=1;i<=n;i++) scanf("%s",a[i]);
qsort(a+1,n,sizeof(a[1]),cmp);
for (int i=1;i<=n;i++) printf("%s",a[i]);
printf("\n");
}
return 0;
}