您的位置:首页 > 其它

UVA 10905 Children's Game

2014-04-13 12:29 435 查看
刚开始做这题,没想到用sort排序,也没想到用string来存储输入的数字,弄的特别麻烦。现在看来,只要弄懂sort自定义排序方法,就很好做了。

sort所在头文件:
#include <algorithm>

首先,sort默认的是升序排序

好在,可以自己写一个cmp函数,使sort按指定意图进行排序

譬如:

bool cmp(int a,int b)
{
return a>b;
}
sort(a,a+100,cmp);



int cmp( const int &a, const int &b ){
if( a > b )
return 1;
else
return 0;
}
sort(a,a+n,cmp);

都是对数组a降序排序

又如:

int cmp( const POINT &a, const POINT &b ){
if( a.x < b.x )
return 1;
else
if( a.x == b.x ){
if( a.y < b.y )
return 1;
else
return 0;
}
else
return 0;
}
sort(a,a+n,cmp);

是先按x升序排序,若x值相等则按y升序排

刚开始看不懂意思,多看几个就找出规律了。

下面是源代码

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int n;
string str[55];
int cmp(string a,string b)
{
return a+b>b+a;
}
void solve()
{
for(int i=0; i<n; i++)
cin>>str[i];
sort(str,str+n,cmp);
for(int i = 0; i < n; i++) cout<<str[i];
}

int main()
{
while(cin>>n&&n)
{
solve();
cout<<endl;
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UVA 10905 Childrens