贪心:Children's Game UVA - 10905

贪心策略:就是s1+s2>s2+s1这个贪心策略非常容易发现。

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int maxn = 55;
bool cmp(string s1, string s2){
    return s1 + s2 > s2 + s1;
}
string a[maxn];
int n;
int main(){
    while (cin >> n){
        if (!n)break;
        for (int i = 0; i < n; ++i)
            cin >> a[i];
        sort(a, a + n, cmp);
        for (int i = 0; i < n; ++i)
            cout << a[i];
        cout << endl;
    }
}

贪心:Children's Game UVA - 10905

原文地址:https://www.cnblogs.com/ALINGMAOMAO/p/10909143.html

时间: 2024-12-24 06:40:05

贪心:Children's Game UVA - 10905的相关文章

uva 10905 Children&#39;s Game(排序或者有点贪心)

今天配置vim没有成功,老是显示什么error,唉,其实之前成功过的,只不过是重装了dev,然后就变了,可能环境 变量的问题,但是我都改了的啊,以后再调吧... 这道题其实不是我想出来的看的题解,又看题解了...好吧,既然看了题解就得好好掌握才是.用到了我刚刚在 c++ primer里面学的string类,挺好用的,以后我准备写程序尽量用c++内容,多练练.. 又加深理解了qsort调用的cmp函数,它的两个参数其实都是要比较的元素的指针,比如这道题中的元素是string类 型,那么他们都是st

UVA - 10905 - Children&#39;s Game (简单排序)

UVA - 10905 Children's Game Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description 4thIIUCInter-University Programming Contest, 2005 A Children's Game Input: standard input Output: standard output Problems

uva 10905 Children&#39;s Game

题意:给n个数字,将它们重新排序得到一个最大的数字, 分析:写一个比较函数每次调用,比较a+b>b+a; 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<algorithm> 5 using namespace std; 6 7 string s[60]; 8 int n; 9 10 bool cmp(string a,string b) 11 { 12 ret

uva 10905 Children&#39;s Game (用String的话,就是水题)

本以为string会耗时,就用数组,结果老是WA,没了耐心找错误,就换成string,秒过! #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <string> #include <stack> #include <cmath> #include <queue> #include <

【字符串排序,技巧!】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 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 in

UVA 10905

这题一开始比较错了.两字符串比较应该是 ab和ba两字符串连接起来比较,谁在前面大就排前面. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct Num{ char str[1000]; }num[60]; char tmp1[1000],tmp2[1000]; bool cmp(Num s,Num

贪心思维 专题记录 2017-7-21

A.UVa 10382 - Watering Grass 题目大意: 有一块草坪,长为l,宽为w,在它的水平中心线上有n个位置可以安装喷水装置,各个位置上的喷水装置的覆盖范围为以它们自己的半径ri为圆.求出最少需要的喷水装置个数. 思路 :转化一下 将二维降成一维      d = sqrt(1.0*r*r-w*w/4.0) 接着就是区间覆盖问题了 #include <bits/stdc++.h> using namespace std; const int maxn = 10000+10;

2017暑假 贪心

uva 10382 - Watering Grass 题目地址: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1323 洒水器洒水区域是圆形,草地是矩形,勾股定理求出洒水器所撒横向范围然后贪心就好了. ps:如果洒水器洒水直径 r*2 小于草地宽度 w ,这个装置就非常很超级没用,这种数据就不需要处理了,否则会tle... 代码如下:

大白书第一章

UVA 11292 The Dragon of Loowater(简单贪心) 题意: n条恶龙,m个勇士,用勇士来杀恶龙.一个勇士只能杀一个恶龙.而且勇士只能杀直径不超过自己能力值的恶龙.每个勇士需要支付能力值一样的金币.问杀掉所有恶龙需要的最少金币. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 20000 + 5; 4 int n, m; 5 int night[maxn], dragon[maxn