Children's Game

Problem Description

There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game here. Each player will be given N positive integer. (S)He can make a big integer by appending those integers after one another. Such as if there are 4 integers as 123, 124, 56, 90 then the following integers can be made – 1231245690, 1241235690, 5612312490, 9012312456, 9056124123 etc. In fact 24 such integers can be made. But one thing is sure that 9056124123 is the largest possible integer which can be made.

Input

Each input starts with a positive integer N (≤ 50). In next lines there are N positive integers. Input is terminated by N = 0, which should not be processed.

Output

For each input set, you have to print the largest possible integer which can be made by appending all the N integers.

Sample Input

4
123 124 56 90
5
123 124 56 90 9
5
9 9 9 9 9
0

Sample Output

9056124123
99056124123
99999 题目链接:https://vjudge.net/problem/UVA-10905

基本思路就是:按照最优顺序排序
这里的最优顺序就是尽可能的保证:权高的位获得数字大的数
实现:当时我想到的只是使用 string的重载运算符 > 来排序。
string s1>string s2 : 根据字典序比较
一般的 :123 124 56 90
字典序排序后 :90 56 124 123 正确
但例如 :9421 942 234 123
字典序排序后 :9421 942 234 123 错误
正确的 :942 9421 234 123 正确
排序规则:s1+s2>s2+s1 ps:(bool cmp() 规定的是一个小于的规则)
一般的 942 4234
942 4234>4234 942
之前的 9421 942
942 9421>9421 942

AC代码如下:
 #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAXN=;
string ss[MAXN];
bool cmp(string s1,string s2)
{//规定一个小于的规则 true:s[1],s[2] 代入后为真。
return s1+s2>s2+s1;
}
int main()
{
int n;
while(cin>>n&&n)
{
for(int i=;i<n;i++)
cin>>ss[i];
sort(ss,ss+n,cmp);
for(int i=;i<n;i++)
cout<<ss[i];
cout<<endl;
}
return ;
}

2017-03-06 13:27:42

UVA 10905 Children's Game (贪心)的更多相关文章

  1. UVA 10905 Children's Game 孩子的游戏 贪心

    题意:给出N个数,要求把它们拼凑起来,让得到的数值是最大的. 只要分别比较两个数放前与放后的值的大小,排序后输出就可以了. 比如123和56,就比较12356和56123的大小就行了. 写一个比较函数 ...

  2. uva 10905 Children's Game (排序)

    题目连接:uva 10905 Children's Game 题目大意:给出n个数字, 找出一个序列,使得连续的数字组成的数值最大. 解题思路:排序,很容易想到将数值大的放在前面,数值小的放在后面.可 ...

  3. UVA 10905 Children's Game (贪心)

    贪心,假如任意给出一个序列,如果两两交换了以后会变大,那么就交换,直到不能交换为止. #include<bits/stdc++.h> using namespace std; ; stri ...

  4. UVa 10905 - Children's Game(求多个正整数排列后,所得的新的数字的极值)

    4thIIUCInter-University Programming Contest, 2005 A Children’s Game Input: standard input Output: st ...

  5. 【字符串排序,技巧!】UVa 10905 - Children’s Game

    There are lots of number games for children. These games are pretty easy to play but not so easy to ...

  6. UVa 10905 - Children's Game 排序,题目没有说输入是int 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  7. UVa 10905 Children's Game

    注意!这不是单纯的字典序排序,比如90.9,应该是990最大 对字符串排序蛋疼了好久,因为别人说string很慢,所以一直没有用过. 看别人用string还是比较方便的,学习一下 对了,这里的cmp函 ...

  8. Children's Game UVA - 10905

    看90,956这样的串,在比较完之前,就确定大小的,必定选大的放在前.而x=98,y=980;这样的,比较x+y和y+x的大小.如果x+y更小,y就放前. #include <iostream& ...

  9. 【NOIP合并果子】uva 10954 add all【贪心】——yhx

    Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvesconde ...

随机推荐

  1. Android开发遇到短信备份失败

    今天做了一个有关ContentProvider的短信备份的小案例,遇到短信备份失败,费了一番周折后终于找到了问题所在 该案例是将短信写到一个xml文件然后保存在手机存储中实现短信的备份功能,关键实现代 ...

  2. 给angularJs grid列上添加自定义按钮

    由于项目需要在angular 显示的表格中添加按钮,多次查询资料终于找到解决方法.就是给columnDefs 上的列增加 cellTemplate,同时绑定对应的触发事件,代码如下 columnDef ...

  3. AutoMapper.RegExtension[.NET Core版本] 介绍

    Technorati 标签: AutoMapper.RegExtension,AutoMapper.RegExtension .NET CORE AutoMapper.RegExtension 为一个 ...

  4. python爬虫实战(二)--------千图网高清图

    相关代码已经修改调试----2017-3-21 实现:千图网上高清图片的爬取 程序运行20小时,爬取大约162000张图片,一共49G,存入百度云.链接:http://pan.baidu.com/s/ ...

  5. 分布式统一配置平台-Disconf.Net

    源码地址:https://github.com/qkbao/Disconf.Net 作者:青客宝  联系qq:后续奉上 为了更好的解决分布式环境下多台服务实例的配置统一管理问题,本文提出了一套完整的分 ...

  6. swfobject.js加载swf,关于是否加加载完成;

    这几个帖子可以参考下 http://bbs.9ria.com/forum.php?mod=viewthread&page=1&tid=38913 http://bbs.9ria.com ...

  7. 判断是否是IE(包含IE11)

    判断是否是IE(包含IE11) if(!!window["ActiveXObject"] || "ActiveXObject" in window) { ale ...

  8. 老李案例分享:MAT分析应用程序服务出现内存溢出过程

    老李案例分享:MAT分析应用程序服务出现内存溢出过程   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在poptest的loa ...

  9. Xmpp实现简单聊天系列 --- ①openfire部署

    1. 下载最新的openfire安装文件 官方下载站点:http://www.igniterealtime.org/downloads/index.jsp#openfire 2. 下载完成后,执行你的 ...

  10. VS 2017开发ASP.NET Core Web应用过程中发现的一个重大Bug

    今天试着用VS 2017去开发一个.net core项目,想着看看.net core的开发和MVC5开发有什么区别,然后从中发现了一个VS2017的Bug. 首先,我们新建项目,ASP.NET Cor ...