uva 10905 Children's Game(排序或者有点贪心)

今天配置vim没有成功,老是显示什么error,唉,其实之前成功过的,只不过是重装了dev,然后就变了,可能环境

变量的问题,但是我都改了的啊,以后再调吧。。。

这道题其实不是我想出来的看的题解,又看题解了。。。好吧,既然看了题解就得好好掌握才是。用到了我刚刚在

c++ primer里面学的string类,挺好用的,以后我准备写程序尽量用c++内容,多练练。。

又加深理解了qsort调用的cmp函数,它的两个参数其实都是要比较的元素的指针,比如这道题中的元素是string类

型,那么他们都是string类型的指针。。。比较一下就ok啦,返回值是1就是升序排列,-1是降序排列。。。

其实百度百科中的qsort讲的挺详细的,可以参考参考。。

解法:

其实就是在排序的时候多了点变化,把a+b和b+a的较大值排到前面就可以了,+在string类中可以是字符串的连接,

用二维字符数组也可以,看到了一个人写的在cmp函数里面加了几个字符复制函数,然后比较一下,其实还是比较的a+b和b+a。。。

代码:

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<string>
using namespace std;
string str[55];
int cmp(const void *a,const void *b)
{
	return *(string *)a +*(string *)b < *(string *)b + *(string *)a;
}
int main()
{
	int i,n;
	while(cin >> n, n)
	{
		for(i=0; i<n; i++)
			cin >> str[i];
		qsort(str,n,sizeof(str[0]),cmp);
		for(i=0; i<n; i++)
			cout << str[i];
		cout << endl;
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

uva 10905 Children's Game(排序或者有点贪心)

时间: 2024-08-06 03:42:46

uva 10905 Children's Game(排序或者有点贪心)的相关文章

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

贪心:Children&#39;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

UVa 10305 - Ordering Tasks 拓扑排序题解

Topological Sort题解.本题是简单的入门题目. Topological Sort的思想很简单,就是按没有入度的点,先输出,然后删除这个点的出度.然后输出下一组没有入度的点. 如何实现也是很简单的: 这里使用邻接表,建图的时候反过来建图,建立一个入度邻接表. 然后使用一个vis数组,记录访问过的节点,也可以根据这个信息知道哪些是已经输出的点,这个时候这些点的入度可以不算为当前入度了. #include <stdio.h> #include <vector> using

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

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosmsg=Submission+received+with+ID+1708713 /** UVA LA 7146 2014上海亚洲赛(贪心) 题目大意:给定敌我双方士兵的数量和每个士兵的攻击力和防守力,如果两个士兵对战,一方

UVA 1572 Self-Assembly(拓扑排序)

1 // 把一个图的所有结点排序,使得每一条有向边(u,v)对应的u都排在v的前面. 2 // 在图论中,这个问题称为拓扑排序.(toposort) 3 // 不难发现:如果图中存在有向环,则不存在拓扑排序,反之则存在. 4 // 不包含有向环的有向图称为有向无环图(DAG). 5 // 可以借助DFS完成拓扑排序:在访问完一个结点之后把它加到当前拓扑序的首部. 6 7 int c[maxn]; 8 int topo[maxn],t; 9 bool dfs(int u) 10 { 11 c[u]

uva 11039 Building designing (排序)

uva 11039 Building designing An architect wants to design a very high building. The building will consist of some floors, and each floor has a certain size. The size of a floor must be greater than the size of the floor immediately above it. In addit