zoukankan      html  css  js  c++  java
  • UVA 10905 Children's Game (贪心)

    Children's Game

    Problem Description

    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.

    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

    题目链接:https://vjudge.net/problem/UVA-10905

    基本思路就是:按照最优顺序排序
    这里的最优顺序就是尽可能的保证:权高的位获得数字大的数
    实现:当时我想到的只是使用 string的重载运算符 > 来排序。
    string s1>string s2 : 根据字典序比较
    一般的 :123 124 56 90
    字典序排序后 :90 56 124 123 正确
    但例如 :9421 942 234 123
    字典序排序后 :9421 942 234 123 错误
    正确的 :942 9421 234 123 正确
    排序规则:s1+s2>s2+s1 ps:(bool cmp() 规定的是一个小于的规则)
    一般的 942 4234
    942 4234>4234 942
    之前的 9421 942
    942 9421>9421 942

    AC代码如下:
     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 using namespace std;
     5 const int MAXN=52;
     6 string ss[MAXN];
     7 bool cmp(string s1,string s2)
     8 {//规定一个小于的规则 true:s[1],s[2] 代入后为真。
     9     return s1+s2>s2+s1;
    10 }
    11 int main()
    12 {
    13     int n;
    14     while(cin>>n&&n)
    15     {
    16         for(int i=0;i<n;i++)
    17             cin>>ss[i];
    18         sort(ss,ss+n,cmp);
    19         for(int i=0;i<n;i++)
    20             cout<<ss[i];
    21         cout<<endl;
    22     }
    23     return 0;
    24 }

    2017-03-06 13:27:42

  • 相关阅读:
    ViewState
    jar包签名
    Eclipse打JAR包引用的第三方JAR包找不到 问题解决
    java项目打jar包
    像VS一样在Eclipse中使用(拖拉)控件
    Myeclipse buildpath 加server library
    nativeswing的关闭问题 当出现Socket连接未断开错误
    Windows 7 配置jdk 1.7环境变量
    myeclipse添加server library
    RichFaces 大概
  • 原文地址:https://www.cnblogs.com/Twobox/p/6509541.html
Copyright © 2011-2022 走看看