zoukankan      html  css  js  c++  java
  • UVA 10905 Children's Game 孩子的游戏 贪心

    题意:给出N个数,要求把它们拼凑起来,让得到的数值是最大的。

    只要分别比较两个数放前与放后的值的大小,排序后输出就可以了。

    比如123和56,就比较12356和56123的大小就行了。

    写一个比较函数,然后用sort调用就行了。

    刚开始时用long long做,每次比较都让数相连,然后比较大小,后来发现数据好像会很暴力,即使longlong也不够大。

    考虑到两个数相连后两种情况长度都一样,所以只要把数值当成string来做就行了,比较两个string的字典序大小。

    代码:

     /*
     *   Author:        illuz <iilluzen@gmail.com>
     *   Blog:          http://blog.csdn.net/hcbbt
     *   File:          uva10905.cpp
     *   Lauguage:      C/C++
     *   Create Date:   2013-08-25 09:24:23
     *   Descripton:    UVA 10905 Children's Game, greed
     */
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <iostream>
    #include <list>
    #include <vector>
    #include <map>
    #include <set>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <utility>
    #include <algorithm>
    using namespace std;
    #define rep(i, n) for (int i = 0; i < (n); i++)
    #define repu(i, a, b) for (int i = (a); i < (b); i++)
    #define repf(i, a, b) for (int i = (a); i <= (b); i++)
    #define repd(i, a, b) for (int i = (a); i >= (b); i--)
    #define swap(a, b) {int t = a; a = b; b = t;}
    #define mc(a) memset(a, 0, sizeof(a))
    #define ms(a, i) memset(a, i, sizeof(a))
    #define sqr(x) ((x) * (x))
    #define FI(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
    typedef long long LL;
    typedef unsigned long long ULL;
    
    /****** TEMPLATE ENDS ******/
    
    const int MAXN = 55;
    int n;
    string num[MAXN];
    
    bool cmp(const string& a, const string& b) {
    	return a + b > b + a;
    }
    
    int main() {
    	while (scanf("%d", &n) && n) {
    		rep(i, n) cin >> num[i];
    		sort(num, num + n, cmp);
    		rep(i, n) cout << num[i];
    		cout << endl;
    	}
    	return 0;
    }


  • 相关阅读:
    Android发送信息模拟系统
    Android SharedPreferences
    Android中SQLiteDatabase操作【附源码】
    poj 2453
    pku 1020
    poj 2594 Treasure Exploration
    pku 2092 Grandpa is Famous
    zoj Weekend Party
    poj 3259 Wormholes
    poj 2455 Secret Milking Machine
  • 原文地址:https://www.cnblogs.com/pangblog/p/3283542.html
Copyright © 2011-2022 走看看