题意:给出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. cocos2d-x中的尺寸之一

    cocos2d-x中的尺寸函数繁多,官方文档对各种尺寸没有很好的描述,网上文章更加寥寥,对尺寸和位置的理解如果不到位,写代码就非常困难,需要猜测尝试,效率低下.这个文章我将研究一些我所迷惑的尺寸函数, ...

  2. kinect for windows - SkeletonBasics-D2D详解之二

    首先看骨骼追踪例子代码的结构:

  3. TableLayoutPanel 的使用

    VS自带控件TableLayoutPanel的功能应用网上都有资料.下面是使用中的一些问题. 1.动态加载控件时使用SetRowSpan.SetColumnSpan方法会使得界面响应缓慢 解决方案: ...

  4. Proving Equivalences(加多少边使其强联通)

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. 深入了解Nginx之Nginx与Python(1)

    6 Python和Nginx 6.1 简介FastCGI FastCGI(Fast Common Gateway Interface)是基于CGI上的改进,是CGI的一种演变产物.虽然目的是保持同样的 ...

  6. BZOJ 1642: [Usaco2007 Nov]Milking Time 挤奶时间( dp )

    水dp 先按开始时间排序 , 然后dp. dp( i ) 表示前 i 个时间段选第 i 个时间段的最优答案 , 则 dp( i ) = max( dp( j ) ) + w_i ( 0 < j ...

  7. Eclipse之报错信息及其解决方案

    一.有很多人都喜欢开发js的时候用aptana,因此在eclipse中集成aptana插件是必须的,可是,在用link方式在eclipse中安装好aptana后,启动时会报如下错误 An intern ...

  8. JS日历控件

    <input type="text" id="st" name="st" onclick="return Calendar( ...

  9. oracle 开发笔记“跨数据库查询复制”

    1.方法一:创建DBL(data base link) CREATE PUBLIC DATABASE LINK 数据链名称 CONNECT TO 登陆用户名 IDENTIFIED BY 密码 USIN ...

  10. linux小命令

    本文主要记录了我在工作中用到的一些linux命令 1.查询linux系统是32位还是64位 zhaolei@zhaolei :~# file /usr/bin/whoami/usr/bin/whoam ...