您的位置:首页 > 其它

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不了,后来终于明白了。
#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;

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: