UVA 10905 Children's Game 孩子们的游戏 【贪心】

题目链接:

  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21354


贪心

  将数字转化为字符串,排序函数是bool cmp(string s1, string s2){ return s1+s2 > s2+s1; }

  以此保证每两个数字排序后的链接结果都是最优的,最后直接输出即可。


 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<string>
 7 #include<vector>
 8 #include<set>
 9 #include<queue>
10 #include<map>
11 #include<stack>
12 using namespace std;
13 const int MAXL = 55;
14 string num[MAXL];
15 int n;
16 bool cmp(string s1, string s2){ // 规定了“小于”的规则
17     return s1+s2 > s2+s1;
18 }
19 
20 int main(){
21     while(cin>>n){
22         if(!n) break;
23         for(int i = 0; i < n; i++) cin>>num[i];
24         sort(num, num+n, cmp);
25         for(int i = 0; i < n; i++) cout<<num[i];
26         cout<<endl;
27     }
28 
29     return 0;
30 }
原文地址:https://www.cnblogs.com/miaowTracy/p/4851300.html