UVA 10905 Children's Game

来源:互联网 发布:山东男人啪的表现知乎 编辑:程序博客网 时间:2024/06/10 13:29

题目链接:点击打开链接

 表示最近被高中生虐成渣了,不会正确的思考了。

这道题很有意思,之前就有小朋友问我。给一些正整数,按照一定是序列输出,使组成该数最大。。。表示,自己的思路很凌乱。还是看了别人的思路。

思路很有技巧:if(a + b >  b + a)  than a一定排在b前面。a + b是字符串是相加。。好思路。。弱渣只能继续学习。。!!

Code:

#include <iostream>#include <algorithm>#include <string>using namespace std;const int N = 55;bool cmp (string a, string b){    return a + b > b + a;}int main(){    int n;    while(cin >> n && n){        string str[N];        for(int i = 0; i < n; i ++){            cin >> str[i];        }        sort(str, str + n, cmp);        for(int i = 0; i < n; i ++){            cout << str[i];        }        cout << endl;    }    return 0;}


0 0