您的位置:首页 > 其它

HDU 3785- 寻找大富翁

2012-08-03 08:03 274 查看
/*algorithm 是算法的意思
#include <algorithm> 包括各种数据结构的具体元素检索、替换、逆序等等通用的算法
*/
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
if(a>b)
return true; //降序
else
return false;
}
int main()
{
int m,n,i;
int a[100000];
while(cin>>n>>m,n||m)
{
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n,cmp); //排序函数sort,对给定区间(a,a+n)所有元素进行排序 ,此处为降序
for(i=0;i<m;i++)
{
if(i==0)
{cout<<a[i];continue;}
cout<<' '<<a[i];
}
cout<<endl;
}
return 0;
}


#include<stdio.h>
#include<stdlib.h>
int cmp(const void *a,const void *b)
{
return *(int *)b-*(int *)a;//降序
}
int main()
{
int m,n,i;
int a[100000];
while(scanf("%d%d",&n,&m),n||m)
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
qsort(a,n,sizeof(a[0]),cmp);
for(i=0;i<m;i++)
{
if(i==0)
printf("%d",a[i]);
else
printf(" %d",a[i]);
}
putchar('\n');
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: