本文主要是介绍uva-10905Children's Game(贪心),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目: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(贪心)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!