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 ...
随机推荐
- final 、finally 和 finalize()的区别
1. final 是一个关键字.可以修饰数据.方法.类. 1)final 数据:final 用来修饰一个永不改变的编译时常量,或者运行时初始化但是不希望被改变的常量.一个既是 static又是 fin ...
- CSS 去掉点li 的点
转:http://blog.sina.com.cn/s/blog_63b13c300100jyek.html 方法一: <ul> <li style="list-style ...
- SLF4J 的几种实际应用模式--之三:JCL-Over-SLF4J+SLF4J
我们前面已经讲过了 SLF4J 的两种用法:SLF4J+Log4J 和 SLF4J+Logback,那是在比较理想的情况下,所用组件只使用了 SLF4J 这一种统一日志框架的时候.可是 JCL 一直 ...
- synchronized关键字
最近重新梳理了下java的synchronized相关内容,希望能帮助到有需要的朋友们. 主要阐述以下几个问题: 1.非static方法前加synchronized class Demo{ synch ...
- iOS回顾笔记(09) -- Cell的添加、删除、更新、批量操作
iOS回顾笔记(09) -- Cell的添加.删除.更新.批量操作 项目中经常有对UITableViewCell做各种操作的需求: 添加一个新的cell 删除某行cell 刷新cell上某行数据(如修 ...
- 每天学点Java小知识【1】
一 Java标识符和关键字 1.标识符 作用:用来标识类名.变量名.方法名.类型名.数组名.文件名的有效字符序列. 组成规则:由字母.下划线.美元符号和数字组成,且第一个字符不能是数字字符.注意:标识 ...
- CSS3学习笔记(1)-CSS3选择器
p{ font-size: 15px; text-indent: 2em; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid ...
- H5与Android之间的交互
关于Android与JS网页端的交互,网上有很多教程,刚做这功能,参考了多方资料,最终出来后觉得简单,但是为实现的话有诸多小问题,最终效果如下: 现在简单整理一下:(直接贴代码,注释详细,应该能懂的) ...
- 常用JS工具包
/*********** *时间辅助类 ***********/ var DateHelper = { //得到两个时间的差值(天数) DateDiff: function (startDate, e ...
- 前端jquery validate表单验证框架的使用
一.框架本身校验方法的扩展 建议写在页内用于扩展框架本身的一些校验方法, 使用频繁也可以直接在源码上修改 例如扩展手机号码的校验: /*手机号码验证扩展 最新的号码 mobile: class的表示 ...