[Fight] number NOIP

[Fight] number NOIP

topic

Title Description

Has n positive integers (n≤20), they are coupled in a row to form a maximum number of integer.

For example: when n = 3, 3 is coupled to the integer 13,312,343 maximum integer: 34331213

Another example: when n = 4, 4 is coupled to the integer 7,13,4,246 maximum integer: 7424613

Input Format

The first line, a positive integer n.

The second line, n a positive integer.

Output Format

A positive integer representing the largest integer

Sample input and output

Input # 1 replication
. 3
13 is 312 343
Output # 1 replication
34331213

analysis

I did not think to write directly, by ordering it ok, but the comparison function cmp is also a direct ratio. .

bool cmp(string a,string b) {
    return a>b;
    }

I submitted a wrong number. . . Acridine feel right. . . . . . . . . . .

The original pit in which (thanks chiefs solution to a problem, no solution to a problem bigwigs sometimes really can not go on ..)

bool cmp(string a,string b) {
    return a+b>b+a;//自定义排序函数,这一步非常巧妙,
	//假设a=321,b=32;a+b=32132,b+a=32321这样下面sort排下来就是32>321
	//避免出现32132>32321的情况 
}
/*如果这样写:
bool cmp(string a,string b){
    return a>b;
    会发生321>32的情况,具体原因是字符串自己的关系运算是这样设定的 
}*/

After each question must make themselves more answers right angles to try. . This problem is more important than doing. .

Code

#include<iostream>
#include<algorithm>
using namespace std;


string s[22];
int n;

bool cmp(string a,string b) {
    return a+b>b+a;//自定义排序函数,这一步非常巧妙,
	//假设a=321,b=32;a+b=32132,b+a=32321这样下面sort排下来就是32>321
	//避免出现32132>32321的情况 
}
/*如果这样写:
bool cmp(string a,string b){
    return a>b;
    会发生321>32的情况,具体原因是字符串自己的关系运算是这样设定的 
}*/

int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>s[i];
	}
	
	sort(s,s+n,cmp);
	for(int i=0;i<n;i++){
		cout<<s[i];
	}	
	return 0;
}
Published 75 original articles · won praise 1 · views 3664

Guess you like

Origin blog.csdn.net/A793488316/article/details/104509120
Recommended