码迷,mamicode.com
首页 > 其他好文 > 详细

uva-10905Children's Game(贪心)

时间:2014-07-28 16:21:33      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   2014   for   cti   代码   ar   

题目:uva-10905Children‘s Game(贪心)


题目大意:给出N个正整数,问将这N个整数连接后得到的最大的数。


解题思路:排序,将两两连接有AB 或是BA,将如果AB > BA ,那么就将A排在B的后面,反之则反之。


代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int N = 55;
const int M = 10000;
int n;

char str1[M], str2[M];

struct Num {

	char s[M];
}num[N];

int cmp (const Num &a, const Num &b) {

	strcpy (str1, a.s);
	strcat (str1, b.s);
	strcpy (str2, b.s);
	strcat (str2, a.s);
	if (strcmp (str1, str2) > 0)
		return 1;
	return 0;
   /* int l1 = strlen (a.s);
	int l2 = strlen (b.s);

	if (l1 <= l2) {
		
		for (int i = 0, j = 0; j < l2; i = (i + 1) % l1, j++) {
		
			if (b.s[j] != a.s[i])
				return a.s[i] > b.s[j];
		}
	} else {

		for (int i = 0, j = 0; j < l1; i = ( i + 1) % l2, j++) {
			
			if (a.s[j] != b.s[i])
				return a.s[j] > b.s[i];
		}
	}
	return 0;*/
}

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

		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;
}


uva-10905Children's Game(贪心),布布扣,bubuko.com

uva-10905Children's Game(贪心)

标签:blog   http   io   2014   for   cti   代码   ar   

原文地址:http://blog.csdn.net/u012997373/article/details/38224387

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!