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

哇,这个题目好难啊,难死了

看了下别人的解法,觉得很是牛逼,用到了sort函数。

#include<iostream>
#include<string>//不要加.h 
#include<algorithm>
using namespace std;
#define max 51
bool cmp(string s1,string s2)
{
	return s1+s2>s2+s1;
}
int main()
{
	string arr[max];
	int n;
	while(~scanf("%d",&n)&&n){
		int i;
		for(i=0;i<n;i++)
		cin>>arr[i];
		sort(arr,arr+n,cmp);
		for(i=0;i<n;i++)
		cout<<arr[i];
		cout<<endl;
	}
	return 0;
}

这个是看了别人的然后自己打出来的,https://blog.csdn.net/wss_ang/article/details/79490106他那里有俩,真的是觉得很震惊呀,,,讨论了半天,,,应该怎么排序,用数字的规律,但是没有结果,后来看到这个,哇塞,,,

,因为我基础不好,所以我就把我开始没想明白的地方分享一下吧 。见笑见笑。。。

首先就是sort的用法,然后查了下把sort和qsort总结出来了,详见https://blog.csdn.net/wss_ang/article/details/79490106

return 如果s1+s2>s2+s1,就返回真,就换,嗯,神奇。

加油加油

猜你喜欢

转载自blog.csdn.net/weixin_42324904/article/details/81183506
今日推荐