UVA 10905 Children's Game (贪心)
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 (贪心)的更多相关文章
- UVA 10905 Children's Game 孩子的游戏 贪心
题意:给出N个数,要求把它们拼凑起来,让得到的数值是最大的. 只要分别比较两个数放前与放后的值的大小,排序后输出就可以了. 比如123和56,就比较12356和56123的大小就行了. 写一个比较函数 ...
- uva 10905 Children's Game (排序)
题目连接:uva 10905 Children's Game 题目大意:给出n个数字, 找出一个序列,使得连续的数字组成的数值最大. 解题思路:排序,很容易想到将数值大的放在前面,数值小的放在后面.可 ...
- UVA 10905 Children's Game (贪心)
贪心,假如任意给出一个序列,如果两两交换了以后会变大,那么就交换,直到不能交换为止. #include<bits/stdc++.h> using namespace std; ; stri ...
- UVa 10905 - Children's Game(求多个正整数排列后,所得的新的数字的极值)
4thIIUCInter-University Programming Contest, 2005 A Children’s Game Input: standard input Output: st ...
- 【字符串排序,技巧!】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 ...
- UVa 10905 - Children's Game 排序,题目没有说输入是int 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVa 10905 Children's Game
注意!这不是单纯的字典序排序,比如90.9,应该是990最大 对字符串排序蛋疼了好久,因为别人说string很慢,所以一直没有用过. 看别人用string还是比较方便的,学习一下 对了,这里的cmp函 ...
- Children's Game UVA - 10905
看90,956这样的串,在比较完之前,就确定大小的,必定选大的放在前.而x=98,y=980;这样的,比较x+y和y+x的大小.如果x+y更小,y就放前. #include <iostream& ...
- 【NOIP合并果子】uva 10954 add all【贪心】——yhx
Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvesconde ...
随机推荐
- 【WCF】错误处理(二):错误码―—FaultCode
先来说说SOAP消息中错误消息的包装结构,一条SOAP错误消息的大致形式如下: <s:Fault> <faultcode xmlns:a="me-cust-error&qu ...
- js 判断是否为空对象、空数组
当需要判断参数是否为空时,总希望 js 能够提供原生的判断方法,可惜并没有,只能自己封装了. function isEmpty(obj) { // 检验 undefined 和 null if(!ob ...
- C# .NET 逻辑层的框架设计
前述:在我的了解中,一个大项目的逻辑层是不可捉摸的,对于不同项目或场景都是不同的逻辑.先说明,我的想法是对逻辑层类结构,以及如何操作逻辑的方法进行抽象的封装.并且考虑将不同类,或者不同程序集中的逻辑方 ...
- 【树莓派】树莓派下WiFi断线自动重连
实现 WiFi 断线自动重连.原理是用 Python 监测网络是否断线,如果断线则重启网络服务. 1.Python 代码 autowifi.py,放在 /home/pi 目录下: #!/usr/bin ...
- Spring-java代理技术总结
Spring 中采用JDk的动态代理和CGLib代理技术在运行期间织入增强,所以用户不需要装备特殊的编译器或者类装载器就可以使用AOP功能. 要使用jdk的动态代理,目标类必须实现接口,而CGLib代 ...
- synchronized关键字
最近重新梳理了下java的synchronized相关内容,希望能帮助到有需要的朋友们. 主要阐述以下几个问题: 1.非static方法前加synchronized class Demo{ synch ...
- sas2ircu工具信息收集及磁盘定位
最近几台Dell服务器的磁盘损坏,报修厂商之后dell工程师需要手机机器磁盘插槽位置信息,使用的就是sas2ircu工具. 此工具还可以配置RAID信息,但是我这次只需要他的查看信息的功能,下面就开始 ...
- Python之路-Linux命令基础(4)
作业一: 1)开启Linux系统前添加一块大小为15G的SCSI硬盘 2)开启系统,右击桌面,打开终端 3)为新加的硬盘分区,一个主分区大小为5G,剩余空间给扩展分区,在扩展分区上划分1个逻辑分区,大 ...
- POPTEST老李分享session,cookie的安全性以及区别 3
如何查看服务器端输送到我们电脑中的这些Cookie信息: 点开IE浏览器或其他浏览器,在菜单栏中有工具选项,点开有InterNet选项: Cookie名称.来源.文件格式( ...
- 老李分享:大数据框架Hadoop和Spark的异同
poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-845052 ...