UVA10905 Children's Game

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. You may think that it’s very easy to find out the answer but will it be easy for a child who has just got the idea of number?

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

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

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

//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;
}

  

UVA10905 Children's Game

时间: 2024-11-09 01:29:39

UVA10905 Children's Game的相关文章

UVA10905: Children&#39;s Game(排序)

题目:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/A 题目需求:,给n个数字,将它们重新排序得到一个最大的数字,好像给出123 456 789 拼为 789456123 最大 这题可以算是一个排序题,不过排序的规则有讲究 如果想用字典序排序,显然错了,好像999123 999 , 按字典序排序999123在前面,得到的数字为999123999 , 显然没有不够999999123 大 题目解析:冒泡排序,首

贪心专题(不定期更新)

1.UVa 10382 - Watering Grass (贪心-区间覆盖) 题意:一块矩形草坪,给出若干个分布在中轴线上的喷水装置,喷水范围为圆形.问能否覆盖整个草坪,若能,求出最少的喷水装置数目. 思路:圆形半径小于等于草坪宽度一半的不用考虑:在剩下的喷水圆形区域中,求出对应覆盖草坪的矩形区块,之后则进行贪心处理:找出矩形区块[L,R]左侧L小于等于当前已覆盖区域的右侧POS的区块中,右侧R最大的那个,同时更新POS. 1 #include<iostream> 2 #include<

【一坨理论AC的题】Orz sxy大佬

1.UVA10891 Game of Sum 2.LA4254 Processor . 3.UVA10905 Children's Game 4.UVA11389 The Bus Driver Problem 5.LA4094 WonderTeam

children childNodes 的区别

1,childNodes 属性,标准的,它返回指定元素的子元素集合,包括HTML节点,所有属性,文本.可以通过nodeType来判断是哪种类型的节点,只有当nodeType==1时才是元素节点,2是属性节点,3是文本节点. 2,children 属性,非标准的,它返回指定元素的子元素集合.经测试,它只返回HTML节点,甚至不返 回文本 节点.且在所有浏览器下表现惊人的一致.和childNodes 一样,在Firefox下不支持()取集合元素.因此如果想获取指定元素的第一个HTML节点,可以使用c

javascript中的子节点查找,childNodes和children

一.查找元素: 1.getElementById():接收一个参数:要取得的元素的 ID.如果找到相应的元素则返回该元素,如果不存在带有相应 ID 的元素,则返回 null.严格匹配,包括大小写. IE7 及较低版本还为此方法添加了一个有意思的"怪癖": name 特性与给定 ID 匹配的表单元素(<input>.<textarea>. <button>及<select>)也会被该方法返回.如果有哪个表单元素的 name 特性等于指定的

children

<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> </head> <body> <div id="cotent"> <div>我的主页</div> <div>聆听我的</div> <div>私人领地</div> </div> <scrip

jQuery基础学习8——层次选择器children()方法

$('body > div').css("background","#bbffaa"); //和children()方法是等价的,父子关系,和parent()方法是对立的 $('body').children('div').css("background","#bbffaa"); 和$('body > div')是等价的 $('body').children().css("background"

jquery遍历之children()与find()的区别

hildren(selector) 方法是返回匹配元素集合中每个元素的所有子元素(仅儿子辈).参数可选,添加参数表示通过选择器进行过滤,对元素进行筛选. .find(selector)方法是返回匹配元素集合中每个元素的后代.参数是必选的,可以为选择器.jquery对象可元素来对元素进行筛选. .find() 与 .children() 方法类似,不同的是后者仅沿着 DOM 树向下遍历单一层级.这里的children,我理解为儿子,只在儿子这一级遍历.看下例子: [html] view plain

hdu 4001 To Miss Our Children Time( sort + DP )

To Miss Our Children Time Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 4075    Accepted Submission(s): 1063 Problem Description Do you remember our children time? When we are children, we are