题意:输入几个数字,这几个数字按照不同顺序可以组成一个大数,输出这个大数。
方法:刚开始按照字典序排列果断跪了,90和9这样的会出错,应该9在前,字典序9在后,看了别人的博客知道了一种方法,比较909和990,return回去就行,详见代码。
参考:http://blog.csdn.net/sambrown123/article/details/9039061
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
using namespace std;
#define MAX 100+2
int cmp(const void *a, const void *b)
{
char s1[500], s2[500];
if (strlen((char * )a) == strlen((char *)b))
return strcmp((char *)b, (char *)a);
else
{
strcpy(s1, (char *)a);
strcat(s1, (char *)b);
strcpy(s2, (char *)b);
strcat(s2, (char *)a);
return strcmp(s2, s1);
}
}
int main()
{
#ifdef Local
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#endif
int i = 0, n = 0;
char num[MAX][MAX];
while (cin >> n && n)
{
for (i = 0; i < n; i++)
cin >> num[i];
qsort(num, n, sizeof(num[0]), cmp);
for (i = 0; i < n; i++)
cout << num[i];
cout << endl;
}
}
uva - 10905 - Children's Game(贪心,qsort排序)
原文:http://blog.csdn.net/u013545222/article/details/18925095