Ultraviolet A 10905-children's game

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.