您的位置:首页 > 其它

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

2015-10-01 17:45 232 查看
题目链接:

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

贪心

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

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

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<stack>
using namespace std;
const int MAXL = 55;
string num[MAXL];
int n;
bool cmp(string s1, string s2){ // 规定了“小于”的规则
return s1+s2 > s2+s1;
}

int main(){
while(cin>>n){
if(!n) break;
for(int i = 0; i < n; i++) cin>>num[i];
sort(num, num+n, cmp);
for(int i = 0; i < n; i++) cout<<num[i];
cout<<endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: