标签:inter cout set math main family 最大的 logs getc
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
题意:给定n个正整数,把它们连接成一个最大的整数.比如,123,124,556,90有24种连接方法,最大的结果为9 056 124 123。
贪心。一开始就想用string水过。注意不能直接用string的一般默认比较方式(字典序),如90和9,应该比较x+y与y+x
//Serene #include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> using namespace std; const int maxn=50+5; int n; string s[maxn]; int aa;char cc; int read() { aa=0;cc=getchar(); while(cc<‘0‘||cc>‘9‘) cc=getchar(); while(cc>=‘0‘&&cc<=‘9‘) aa=aa*10+cc-‘0‘,cc=getchar(); return aa; } bool cmp(const string x,const string y) { return x+y>y+x; } int main() { n=read(); while(n){ for(int i=1;i<=n;++i) cin>>s[i]; sort(s+1,s+n+1,cmp); for(int i=1;i<=n;++i) cout<<s[i]; printf("\n"); n=read(); } return 0; }
标签:inter cout set math main family 最大的 logs getc
原文地址:http://www.cnblogs.com/Serene-shixinyi/p/7604825.html