Question link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 113 & page = show_problem & problem = 1846
Type: Sort
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 givenNPositive
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,124 1235690, 5612312490,901 2312456, 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 integerN(≤ 50). In next lines there areNPositive Integers. input is terminatedN= 0, which shold
Not be processed.
Output
For each input set, you have to print the largest possible integer which can be made by appending allNIntegers.
Question:
Enter a string of numbers and the output must contain the largest number that can be combined.
Analysis and Summary:
To maximize a number, make it as high as possible. Therefore, it is easy to sort the numbers in Lexicographic Order (think of them as strings) from large to small, and then directly output them.
I did this, and then wa.
The reason is, for example, the number of these three numbers: 12,121 1, 11, after the direct sorting is 1219, 12191211, get 12121911, but the correct sorting should be, 11.
When sorting, if the number of digits is equal, you can directly compare them in lexicographically. If they are not equal, for example, 1219 and 12, then this is not the case. How can we determine it? Obviously, the two numbers are either the first one or the second one, and the two numbers are compared directly to the larger ones.
/* * UVa 10905 - Children's Game * Time : 0.176s (UVa) * Author: D_Double */#include<iostream>#include<cstdio>#include<string>#include<algorithm>using namespace std;typedef string BigNum;BigNum arr[52];bool cmp (const string & a, const string & b){if(a.size()==b.size()) return a > b;string tmp1 = a+b , tmp2 = b+a;return tmp1 > tmp2; }int main(){freopen("input.txt","r",stdin);int n, i;while(scanf("%d",&n), n){for(i=0; i<n; ++i) cin >> arr[i];sort(arr, arr+n, cmp);for(i=0; i<n; ++i) cout << arr[i];printf("\n");}return 0;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
, By d_double (reprinted, please mark)