UVA - 10905 - Children's Game (简单排序)

UVA - 10905

Children‘s Game

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

Description


4thIIUCInter-University Programming
Contest, 2005


A


Children’s Game


Input: standard input

Output: standard output


Problemsetter: Md. Kamruzzaman

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 Npositive
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


Output for Sample Input


4

123 124 56 90

5

123 124 56 90 9

5

9 9 9 9 9

0


9056124123

99056124123

99999

Source

Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Data Structures and Libraries :: Linear Data Structures with Built-in Libraries :: C++
STL algorithm (Java Collections)

Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 1. Algorithm Design :: General Problem Solving Techniques ::Exercises:
Intermediate

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 4. Algorithm Design

Root :: Prominent Problemsetters :: Md. Kamruzzaman (KZaman)

Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Data Structures and Libraries :: Linear Data Structures with Built-in Libraries :: C++
STL algorithm (Java Collections)

找对排序方法就好了!

AC代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

string num[55];

bool cmp(string a, string b)
{
	return a+b > b+a;     //‘+’表示a和b的连接
}

int main()
{
	int n;
	while(scanf("%d", &n), n)
	{
		for(int i=0; i<n; i++)
		{
			cin >> num[i];
		}
		sort(num, num+n, cmp);
		for(int i=0; i<n; i++)
		{
			cout << num[i];
		}
		cout << endl;
	}
	return 0;
}

UVA - 10905 - Children's Game (简单排序)

时间: 2024-10-11 18:25:43

UVA - 10905 - Children's Game (简单排序)的相关文章

uva 10905 Children&#39;s Game(排序或者有点贪心)

今天配置vim没有成功,老是显示什么error,唉,其实之前成功过的,只不过是重装了dev,然后就变了,可能环境 变量的问题,但是我都改了的啊,以后再调吧... 这道题其实不是我想出来的看的题解,又看题解了...好吧,既然看了题解就得好好掌握才是.用到了我刚刚在 c++ primer里面学的string类,挺好用的,以后我准备写程序尽量用c++内容,多练练.. 又加深理解了qsort调用的cmp函数,它的两个参数其实都是要比较的元素的指针,比如这道题中的元素是string类 型,那么他们都是st

uva 10905 Children&#39;s Game

题意:给n个数字,将它们重新排序得到一个最大的数字, 分析:写一个比较函数每次调用,比较a+b>b+a; 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<algorithm> 5 using namespace std; 6 7 string s[60]; 8 int n; 9 10 bool cmp(string a,string b) 11 { 12 ret

uva 10905 Children&#39;s Game (用String的话,就是水题)

本以为string会耗时,就用数组,结果老是WA,没了耐心找错误,就换成string,秒过! #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <string> #include <stack> #include <cmath> #include <queue> #include <

【字符串排序,技巧!】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 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 in

贪心:Children&#39;s Game UVA - 10905

贪心策略:就是s1+s2>s2+s1这个贪心策略非常容易发现. #include<iostream> #include<algorithm> #include<string> using namespace std; const int maxn = 55; bool cmp(string s1, string s2){ return s1 + s2 > s2 + s1; } string a[maxn]; int n; int main(){ while

简单排序

冒泡排序 选择排序 插入排序 时间复杂度 O(n^2) 冒泡排序è基本不用  过于简单比较次数(见下) 与 交换次数(平均为比较次数的一半) 均正比于N^2 选择排序è快于冒泡 交换时间级别大于比较时间级别选择该排序 比较次数(见下) 正比于N^2 交换次数N 插入排序 VS表插入排序 效率比较 插入排序>(略快于)选择排序>冒泡排序 插入排序 表插入排序 原理 简单排序之一 利用有序链表插入效率的比较为O(N)的特点 比较次数MAX (N^2-N)/2 N 比较次数AVG (N^2-N)/4

JavaScript 实现页面元素(ul-li)的简单排序

JavaScript 实现页面元素(ul-li)的简单排序 html页面: <input type="button" value="show" onclick="show()" /> <ul id="ul1"> <li>4</li> <li>3</li> <li>2</li> <li>1</li> </

排序算法之简单排序算法

一.前言 其实排序对于前端工程师来说没有后端那么重要,不过理解排序的原理还是比较有用的.例如有些应用数据比较少量的就不需要用后台来排序,如果用php后端脚本等排序会增加后台的开销.放在前端岂不是更好,当然我们有一个前端的排序神器sort.当然也做了很多得优化,不过用sort排序还需要注意一点,sort默认不传参数时候的排序是按照字母顺序对数组中的元素进行排序,其实就是按照字符编码的顺序进行排序.        今天主要复习一下简单得排序,分别为冒泡排序.插入排序.选择排序,冒泡目前是我认为最简单

Java数据结构和算法之数组与简单排序

一.数组于简单排序 数组 数组(array)是相同类型变量的集合,可以使用共同的名字引用它.数组可被定义为任何类型,可以是一维或多维.数组中的一个特别要素是通过下标来访问它.数组提供了一种将有联系的信息分组的便利方法. 一维数组 一维数组(one‐dimensional array )实质上是相同类型变量列表.要创建一个数组,你必须首先定义数组变量所需的类型.通用的一维数组的声明格式是: type var‐name[ ]; 获得一个数组需要2步: 第一步,你必须定义变量所需的类型. 第二步,你必