UVa 10905:Children's Game

题目链接:

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

类型: 排序

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 theN integers.

题目大意:

输入一串数字, 要求输出有这些数字能拼成的最大的数字

分析与总结:

更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

要让一个数字最大的话,就要让它的高位尽量的大。所以容易可以想到直接对这些数字按字典序(把他们看成字符串)从大到小进行排序,然后直接输出了。

我真的这么做了, 然后,就WA了。

原因在于, 例如这三个数: 12, 1211, 11,   直接排序后是1219, 12,11, 得到12191211,  但是正确的排序应该是12,1219,11, 为12121911。    

进行排序时,如果位数相等的话,那么直接可以字典序比较。 不相等的话,例如1219和12, 那么就不能这样了。那怎样判断呢?很明显,这两个数要么第一个在前要么就是第二个在前, 直接比较这两种情况那个更大即可。

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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索string
, include
, 排序
, 数字
, integer
直接
galgame、3dmgame、c5game、战争前线00game、game center,以便于您获取更多的相关知识。

时间: 2024-11-05 12:33:56

UVa 10905:Children's Game的相关文章

UVa 10905 Children&#039;s Game 解法

题目:给出一组数,把这些数连接起来形成一个大数. 出处:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1846 数据很大,使用long long都会溢出,所以要使用字符代表数值. 一个知识点: 注意比较排序的比较函数 - 就是这里卡了一下,不能是按字典顺序排序,而是要有特殊写法, 如下cmp函数. bool cmp(const string

uva 10905 - Children&#039;s Game

点击打开链接 题目意思:    输入n个数,要求找到一个组合方式,使得值最大输出这个值 解题思路:    自定义cmp函数以及排序应用                      1:这一题的输入的数据当成string处理比较方便,在cmp函数的时候就会非常简洁,但是时间效率不高                      2:题目要求组合成一个最大的数,这个时候我们想到了排序,但是按照平时的排序我们就会发现是错的,所以呢我们就要自己写cmp函数了.具体思路是这样的,比较两个字符串,例如90和56,

UVa 167:The Sultan&#039;s Successors, 八皇后问题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=103 题目类型: 回溯 原题: The Sultan of Nubia has no children, so she has decided that the country will be split into up to k separate

UVa 1422:Processor 任务处理问题

题目大意:有n个任务送到处理器处理,每个任务信息包括r,d,w,r代表开始时间,w代表必须要结束的时间,w指需要多少时间处理. 其中处理器的处理速度可以变速,问处理器最小需要多大速度才能完成工作? 输入: 3 5 1 4 2 3 6 3 4 5 2 4 7 2 5 8 1 6 1 7 25 4 8 10 7 10 5 8 11 5 10 13 10 11 13 5 8 15 18 10 20 24 16 8 15 33 11 14 14 1 6 16 16 19 12 3 5 12 22 25

UVa 10763:Foreign Exchange

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1704 原题: Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very succes

UVa 10341: Solve It

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1282 原题: Solve the equation:        p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0        where 0 <= x <= 1. Input

UVa 10057:A mid-summer night&#039;s dream.

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=998 原题: This is year 2200AD. Science has progressed a lot in two hundred years. Two hundred years is mentioned here because thi

UVa 10487:Closest Sums

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1428 原题: Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two di

UVa 10340:All in All

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1281 原题: You have devised a new encryption technique which encodes a message by inserting between its characters randomly gener