【STL】uva 10905 Children's Game

题意:
   给你n个数字,让你用这n个数组组成一个最大的数字并输出来。


思路:

   这个题目看完第一反应就是直接按照字符串排序,然后轻轻松松写完,交上去直接wa了,为什么会wa呢?感觉也没啥特别的啊,但是看一组数据之后就明白了,9 90组合起来最大是990 而按照字符串排序的话是909因为90>9,所以直接按照字符串排序是错的,那么我们就回到问题的起点,想如果只有两个串a,b的时候我们怎么办,是不是如果a+b>b+a答案是ab否则答案是ba,所有串放在一起呢?也是这个道理,所以直接按照a+b>b+a排序,因为题目没有说每个数字是多少位的,同时string可以直接进项相加和比较,所以我们开string比较方便,但是如果要强行开char数组也行,直接模拟就行了。


///AC代码

#include <iostream>
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <algorithm>
#include <functional>
#define PI acos(-1)
#define eps 1e-8
#define inf 0x3f3f3f3f
#define debug(x) cout<<"---"<<x<<"---"<<endl
typedef long long ll;
using namespace std;


int cmp (string a, string b)
{
    return a + b > b + a;
}
int main()
{

    string str[70];
    int n;
    while (scanf("%d", &n), 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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值