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;
- }
UVA 10905 Children's Game 孩子的游戏 贪心的更多相关文章
- uva 10905 Children's Game (排序)
题目连接:uva 10905 Children's Game 题目大意:给出n个数字, 找出一个序列,使得连续的数字组成的数值最大. 解题思路:排序,很容易想到将数值大的放在前面,数值小的放在后面.可 ...
- 【字符串排序,技巧!】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 ...
- UVA 10905 Children's Game (贪心)
Children's Game Problem Description There are lots of number games for children. These games are pre ...
- UVa 10905 - Children's Game 排序,题目没有说输入是int 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVa 10905 - Children's Game(求多个正整数排列后,所得的新的数字的极值)
4thIIUCInter-University Programming Contest, 2005 A Children’s Game Input: standard input Output: st ...
- UVa 10905 Children's Game
注意!这不是单纯的字典序排序,比如90.9,应该是990最大 对字符串排序蛋疼了好久,因为别人说string很慢,所以一直没有用过. 看别人用string还是比较方便的,学习一下 对了,这里的cmp函 ...
- UVA 10905 Children's Game (贪心)
贪心,假如任意给出一个序列,如果两两交换了以后会变大,那么就交换,直到不能交换为止. #include<bits/stdc++.h> using namespace std; ; stri ...
- UVA LA 7146 2014上海亚洲赛(贪心)
option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosm ...
- luoguP2123 皇后游戏(贪心)
luoguP2123 皇后游戏(贪心) 题目 洛谷题目chuanso 题解 有一篇好题解,我就懒得推式子了,毕竟打到电脑上还是很难的 牛逼题解传送门 code #include<iostream ...
随机推荐
- 转:php+mysql菜单无限级分类(非递归)
php+mysql无限级分类(非递归) 参考:http://www.chhua.com/web-note3244
- Strata 2014 上的 AzureCAT 粉笔会谈
本周,AzureCAT 团队非常高兴在 Strata 会议上首次集体亮相.对于那些对 AzureCAT 团队不太熟悉的人来说,我们是 Microsoft 云与企业部门一个核心的国际性团队,由大约 ...
- 不同服务器之间使用svn钩子post-commit同步代码遇到的证书认证问题.md
遇到的问题,以下其他问题都是因解决这个问题引申出来的问题 VisualSVN hooks自动同步更新到web服务器 错误信息如下: Error validating server certificat ...
- 专门针对初学者的Node.js教程
转载原文:http://www.csdn.net/article/2013-08-28/2816731-absolute-beginners-guide-to-nodejs Node.js的教程并不缺 ...
- 解决:Visual Assist X 不支持HTML、Javascript等提示
Visual Assist X 安装后,不能进行javascript hmtl提示,只有回到老版本才行.这个问题折腾了老久,才给解决了. 记录下来,以便于网友和自己使用. 问题原因: Visual A ...
- [译]SSIS 通过环境变量配置数据源连接参数
场景 希望在包执行的时候可以随意选择参数,这时候我们可以用环境变量 . 另外所有包都可以用环境变量,有大量包的时候就比较方便. Step 1: 创建SSIS包 在Data Flow Task里面创建 ...
- 【转载】如何用Maven创建web项目(具体步骤)
使用eclipse插件创建一个web project 首先创建一个Maven的Project如下图 我们勾选上Create a simple project (不使用骨架) 这里的Packing 选择 ...
- Java学习03
Java学习03 1.java面试一些问题 一.什么是变量 变量是指在程序执行期间可变的数据.类中的变量是用来表示累的属性的,在编程过程中,可以对变量的值进行修改.变量通常是可变的,即值是变化的 二. ...
- HTML5 总结-表单-表单元素
HTML5 表单元素 HTML5 的新的表单元素: HTML5 拥有若干涉及表单的元素和属性. 本章介绍以下新的表单元素: datalist keygen output 浏览器支持 Input typ ...
- MySql 初次安装登陆
名称:随便写 服务器:127.0.0.1或者localhost 端口:在安装mysql应该看到是3306 用户:root 密码:(默认的是空,如果你设置过自己应该知道) 其他就可以不用设置