题意:给出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;
}

UVA 10905 Children's Game 孩子的游戏 贪心的更多相关文章

  1. uva 10905 Children's Game (排序)

    题目连接:uva 10905 Children's Game 题目大意:给出n个数字, 找出一个序列,使得连续的数字组成的数值最大. 解题思路:排序,很容易想到将数值大的放在前面,数值小的放在后面.可 ...

  2. 【字符串排序,技巧!】UVa 10905 - Children’s Game

    There are lots of number games for children. These games are pretty easy to play but not so easy to ...

  3. UVA 10905 Children's Game (贪心)

    Children's Game Problem Description There are lots of number games for children. These games are pre ...

  4. UVa 10905 - Children's Game 排序,题目没有说输入是int 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  5. UVa 10905 - Children's Game(求多个正整数排列后,所得的新的数字的极值)

    4thIIUCInter-University Programming Contest, 2005 A Children’s Game Input: standard input Output: st ...

  6. UVa 10905 Children's Game

    注意!这不是单纯的字典序排序,比如90.9,应该是990最大 对字符串排序蛋疼了好久,因为别人说string很慢,所以一直没有用过. 看别人用string还是比较方便的,学习一下 对了,这里的cmp函 ...

  7. UVA 10905 Children's Game (贪心)

    贪心,假如任意给出一个序列,如果两两交换了以后会变大,那么就交换,直到不能交换为止. #include<bits/stdc++.h> using namespace std; ; stri ...

  8. UVA LA 7146 2014上海亚洲赛(贪心)

    option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosm ...

  9. luoguP2123 皇后游戏(贪心)

    luoguP2123 皇后游戏(贪心) 题目 洛谷题目chuanso 题解 有一篇好题解,我就懒得推式子了,毕竟打到电脑上还是很难的 牛逼题解传送门 code #include<iostream ...

随机推荐

  1. [置顶] LED办公楼宇照明节能方案及城市夜景照明节能方案

    LED照明办公楼宇节能方案 .通用标准灯头,可直接替换现有卤素灯.白炽灯.荧光灯.

  2. haproxy 超时自动重发

    timeout connect 5000 timeout client 50000 timeout server 50000 timeout check 5s stats refresh 30s Ap ...

  3. HDU 4931 Happy Three Friends

    #include <cstdio> #include <algorithm> using namespace std; int p1,p2,a[6],T; int main() ...

  4. Dream

    即使下着雨,天空依旧明亮,因为远方有我的梦想.   ——forever97

  5. [置顶] c++播放Flash文件

    最近由于需要在程序中使用Flash播放,所以学习了下如何播放Flash,这里使用atl库中的CAxWindow来处理我们要播放的Flash!由于Flash的很多接口我们都不知道,所以可以参考前一篇文章 ...

  6. hadoop高速扫盲帖,从零了解hadoop

    1.MapReduce理论简单介绍 1.1 MapReduce编程模型 MapReduce採用"分而治之"的思想,把对大规模数据集的操作,分发给一个主节点管理下的各个分节点共同完毕 ...

  7. java Script 中的keyCode 和charCode

    其实很长一段时间,我都没有完全弄明白keyCode 和charCode ,自己也认真看过,但是就是理解不透彻,为了防止以后再出现混乱,写篇博客记录一下吧! 首先  在不同的浏览器中,他们有不同的说法哦 ...

  8. 简简单单C#爬虫小计

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...

  9. (" use strict")Javascript 严格模式详解

    Javascript 严格模式详解 转载别人的博客内容,浏览了一遍,没有全部吸收,先保存一下链接 http://www.ruanyifeng.com/blog/2013/01/javascript_s ...

  10. 使用R进行相关性分析

    基于R进行相关性分析 一.相关性矩阵计算: [1] 加载数据: >data = read.csv("231-6057_2016-04-05-ZX_WD_2.csv",head ...