二次排序
Problem Description
给定N个整型范围内的整数,要求输出二级排序之后的结果。
Input
输入数据的第一行是一个正整数N(0 < N < 1000)。然后N行数据,每行包含两个整数
h
(
0 < h < 300
)
和
w (0 < w < 200),数字之间
用空格隔开。
Output
你需要把h降序输出,格式跟输入一样,共N行,如果h相同则升序输出,具体格式见样例。
Example Input
8 70 40 180 70 160 150 175 69 180 100 180 75 30 20 169 60
Example Output
180 70 180 75 180 100 175 69 169 60 160 150 70 40 30 20
代码如下:
#include<iostream>
#include <algorithm>
using namespace std;
struct node
{
int h,w;
}arr[100],ar[100];
bool cmp(node a,node b)
{
if(a.h!=b.h) return a.h>b.h;//二次sotr排序部分
return a.w<b.w;
return 0;
}
void f(struct node arr[],int n)
{
struct node t;
int i,j;
for(i=0; i<n-1; i++)
for(j=0; j<n-i-1; j++)
{
if(arr[j].h<arr[j+1].h)//二次冒泡排序部分
{
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
if(arr[j].h==arr[j+1].h)
{
if(arr[j].w>arr[j+1].w)
{
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
}
int main()
{
int n,i;
cin>>n;
for(i=0; i<n; i++)
{
cin>>arr[i].h>>arr[i].w;
ar[i].h=arr[i].h;
ar[i].w=arr[i].w;
}
sort(arr,arr+n,cmp);
cout<<"二次sort排序结果:"<<endl;
for(i=0; i<n; i++)
cout<<arr[i].h<<" "<<arr[i].w<<endl;
f(ar,n);
cout<<"二次冒泡排序结果:"<<endl;
for(i=0; i<n; i++)
cout<<arr[i].h<<" "<<arr[i].w<<endl;
return 0;
}