uva10905-孩子的游戏

题目链接 http://acm.hust.edu.cn/vjudge/problem/21354

 

解题思路

枚举两个串,s1, s2。s1+s2,s2+s1取其中最大的。当所有串对枚举完时答案就出来了。

当然,选取过的串不能再选。

然后就可以看出,这其实是一种排序。

 

代码

#include<iostream>
#include<string>
#include<cstdio>
//#define LOCAL
using namespace std;
const int maxLen = 55;
string s[maxLen];
void Solve(int n)
{
    string ans; string temp;
    for(int i=1; i<n; i++)
        for(int j=0; j<n-i; j++) {
            if(s[j]+s[j+1]<s[j+1]+s[j]) {
                temp = s[j]; s[j]=s[j+1]; s[j+1]=temp;
            }
        }
    for(int i=0; i<n; i++) ans+=s[i];
    cout << ans << endl;
}
int main()
{
    #ifdef LOCAL
        freopen("data.txt", "r", stdin);
        freopen("ans.txt", "w", stdout);
    #endif
    int n;
    cin >> n;
    while(n != 0) {
        for(int i=0; i<n; i++)
            cin >> s[i];
        Solve(n);
        cin >> n;
    }
    return 0;
}

 

posted @ 2016-08-17 09:52  啊嘞  阅读(149)  评论(0编辑  收藏  举报