UVA10905 Children's Game

 

题意:给定n个正整数,把它们连接成一个最大的整数.比如,123,124,556,90有24种连接方法,最大的结果为9 056 124 123。

 

贪心。一开始就想用string水过。注意不能直接用string的一般默认比较方式(字典序),如90和9,应该比较x+y与y+x

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=50+5;
int n;
string s[maxn];
 
int aa;char cc;
int read() {
    aa=0;cc=getchar();
    while(cc<'0'||cc>'9') cc=getchar();
    while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
    return aa;
}
 
bool cmp(const string x,const string y) {
    return x+y>y+x;
}
 
int main() {
    n=read();
    while(n){
        for(int i=1;i<=n;++i) cin>>s[i];
        sort(s+1,s+n+1,cmp);
        for(int i=1;i<=n;++i) cout<<s[i];
        printf("\n");
        n=read();
    }
    return 0;
}

 

  

 

posted @   shixinyi  阅读(128)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
阅读排行:
· 历时 8 年,我冲上开源榜前 8 了!
· 物流快递公司核心技术能力-海量大数据处理技术
· 四大AI编程工具组合测评
· 关于能否用DeepSeek做危险的事情,DeepSeek本身给出了答案
· 如何在 Github 上获得 1000 star?
点击右上角即可分享
微信分享提示