UVa 10905:children ' s Game

Source: Internet
Author: User
Tags sort

Topic Link:

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=1846

Types: Sorting

There are lots of number games for children. These games are pretty easy to play but. We'll discuss about an interesting game. Each player would 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, 1241235690, then the following integers can be made–1231245690, 5612 312490, 9012312456, 9056124123 etc. In fact-such integers can be made. But One thing is sure this 9056124123 is the largest possible an integer which can be made.

You may do it ' s very easy to find out the answer but'll it is easy for a child who has just got the idea of numb Er?

Input

Each input starts with a positive integer N (≤50). In next lines there are N positive integers. The Input is terminated by N = 0 and which should is processed.

Output

For each input set, your have to print the largest possible integer which can is made by appending all TheN.

The main effect of the topic:

Enter a string of numbers that requires the output to be the largest number that can be spelled.

Analysis and Summary:

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

To make the biggest number, make it as high as possible. So it's easy to think of sorting these numbers directly from large to small in dictionary order (see them as strings) and then output directly.

I really did it, then, WA.

The reason is that, for example, these three numbers: 12, 1211, 11, directly sorted after 1219, 12, 11, get 12191211, but the correct sort should be 12,1219,11, 12121911.

When sorting, if the number of digits is equal, then the dictionary can be compared directly. If they are not equal, such as 1219 and 12, then this is not the case. So how do you judge? Obviously, these two numbers are either the first one before or the second, directly comparing the two situations is bigger.

 
 * *  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;  
}

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.