uva 10905 Children's Game (排序)

题目连接:uva 10905 Children's Game


题目大意:给出n个数字, 找出一个序列,使得连续的数字组成的数值最大。


解题思路:排序,很容易想到将数值大的放在前面,数值小的放在后面。可是,该怎么判断数值的大小(判断数值大小不能单单比较两个数的大小,比如遇到:1 、10的情况)。其实,判断一个不行,那就将两个合在一起考虑就可以了(就是比较110合101的大小来普判断1 放前面还是10放前面)。


 

#include <stdio.h>

#include <string.h>

#include <algorithm>

using namespace std;

const int N = 1005;



struct number {

    char s[N]; 

}num[N];



bool cmp(const number &a, const number &b) {

   char str1[N], str2[N];

   strcpy(str1, a.s);

   strcat(str1, b.s);

   strcpy(str2, b.s);

   strcat(str2, a.s);

   return strcmp(str1, str2) > 0;

}



int main() {

    int n;

    while (scanf("%d", &n), n) {

	// Init;

	memset(num, 0, sizeof(num));



	// Read;

	for (int i = 0; i < n; i++)

	    scanf("%s", num[i].s);



	sort(num, num + n, cmp);



	for (int i = 0; i < n; i++)

	    printf("%s", num[i].s);

	printf("\n");

    }

    return 0;

}

 

 

 

你可能感兴趣的:(children)