UVA10905 Children's Game【排序】
2018-03-12 22:25
501 查看
There are lots of number games for children. These games are pretty easy to play but not so easy tomake. 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 are4 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 surethat 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 justgot the idea of number?InputEach input starts with a positive integer N (≤ 50). In next lines there are N positive integers. Inputis terminated by N = 0, which should not be processed.OutputFor each input set, you have to print the largest possible integer which can be made by appending allthe N integers.Sample Input4123 124 56 905123 124 56 90 959 9 9 9 90Sample Output90561241239905612412399999
问题链接:UVA10905 Children's Game。题意简述: 输入n个正整数,将其连成一个最大的整数。
问题分析:(略)程序说明:
本题有三大要点,一是并非把大的整数放在前面,例如12和3,构成的最大整数是312;二是也不能够简单地用函数strcmp()进行比较,例如9和90,构成的最大整数为990而不是909;三是n个整数里,有的可能是大整数,编写了一个C语言的程序,总是AC不了,后来终于明白了。
问题链接:UVA10905 Children's Game。题意简述: 输入n个正整数,将其连成一个最大的整数。
问题分析:(略)程序说明:
本题有三大要点,一是并非把大的整数放在前面,例如12和3,构成的最大整数是312;二是也不能够简单地用函数strcmp()进行比较,例如9和90,构成的最大整数为990而不是909;三是n个整数里,有的可能是大整数,编写了一个C语言的程序,总是AC不了,后来终于明白了。
#include "stdafx.h" #include <cstdio> #include <string> #include <iostream> #include <algorithm> //sort函数在这个头文件中 using namespace std; #define N 50 string aim ; bool cmp(string a, string b) { return a + b > b + a; //string a+b会 自动把 a b 结合成一个字符串的样子. } int main() { int t; int i; while (~scanf_s("%d", &t)&&t) { for (i = 0; i < t; i++) cin >> aim[i]; sort(aim, aim + t, cmp); for (i = 0; i < t; i++) cout << aim[i]; cout << endl; } }
相关文章推荐
- uva 10905 - Children's Game(排序中的贪心)
- uva 10905 Children's Game(排序或者有点贪心)
- (使用STL自带的排序功能进行排序)UVA 10905 Children's Game(求一组数据所能拼出的最大数字串)
- UVA - 10905 Children's Game 字符串的排序
- 【字符串排序,技巧!】UVa 10905 - Children’s Game
- uva 10905 Children's Game 字符串结合排序
- uva 10905 Children's Game (排序)
- [UVA 10905] Children's Game:贪心,排序
- UVa 10905 Children's Game 解题报告(排序)
- UVa 10905 - Children's Game(求多个正整数排列后,所得的新的数字的极值)
- UVa 10905 Children's Game 贪心
- UVa 10905 Children's Game 解法
- UVA 10905 Children's Game 孩子们的游戏 【贪心】
- Children's Game UVA - 10905 贪心
- UVA 10905 Children's Game (贪心)
- Children's Game UVA - 10905 整数按字符串读入
- 4th IIUC Inter-University Programming Contest, 2005 / UVa 10905 Children's Game (等长转化技巧)
- UVA - 10905 Children's Game
- UVA 10905 Children's Game 水 .
- UVA - 10905 Children's Game(贪心)