HDU--2020--排序

输入n(n<=100)个整数,按照绝对值从大到小排序后输出。题目保证对于每一个测试实例,所有的数的绝对值都不相等。

Input

输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整数,n=0表示输入数据的结束,不做处理。

Output

对于每个测试实例,输出排序后的结果,两个数之间用一个空格隔开。每个测试实例占一行。

Sample Input

3 3 -4 2
4 0 1 2 -3
0
Sample Output

-4 3 2
-3 2 1 0
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
bool cmp(int a,int b){
	return a>b;
}
int main(){
	int n,a[100+10],b[100+10];
	while(scanf("%d",&n)!=EOF&&n){
		for(int i=0;i<n;i++){ 
			scanf("%d",&a[i]);
			b[i]=abs(a[i]);	
		} 
		sort(b,b+n,cmp);
  		for(int i=0;i<n-1;i++)
			for(int j=0;j<n;j++)
				if(b[i]==abs(a[j]))
					printf("%d ",a[j]);
		for(int j=0;j<n;j++)
			if(b[n-1]==abs(a[j]))
				printf("%d\n",a[j]);
	}
	return 0;
} 
/*Sample Input
3 3 -4 2
4 0 1 2 -3
0
Sample Output
-4 3 2
-3 2 1 0*/
发布了150 篇原创文章 · 获赞 73 · 访问量 6587

猜你喜欢

转载自blog.csdn.net/queque_heiya/article/details/104248081