Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
给出n件物品,每件物品有质量和价格两种属性。你要做的是按质量升序排序,若质量相同则按价格降序排序。
Input
多组输入。每组先输入一个正整数n(1<=n && n <= 100),代表有n件物品。接下来的一行有n个正整数Wi(1<= Wi && Wi <= 10000),代表每件物品的质量。再接下来的一行有n个正整数Pi(1 <= Pi && Pi <= 10000),代表每件物品的价格。
Output
对于每组数据输出n行,每行两个数Wi,Pi。顺序为题目描述所要求。
Sample Input
3
1 2 2
3 2 3
Sample Output
1 3
2 3
2 2
扫描二维码关注公众号,回复:
8600989 查看本文章
Hint
Source
#include <stdio.h>
#include <stdlib.h>
struct stu
{
int w,price;
} thing[100],t;
int main()
{
int n,i,j;
while(~scanf("%d",&n))
{
for(i=0; i<n; i++)
scanf("%d",&thing[i].w);
for(i=0; i<n; i++)
scanf("%d",&thing[i].price); //输入;
for(i=0; i<n-1; i++) //排序仍然以冒泡排序为基本思路;
{
for(j=0; j<n-i-1;j++)
{
if(thing[j].w>thing[j+1].w)
{
t=thing[j];
thing[j]=thing[j+1];
thing[j+1]=t;
}
//加一个限制条件,质量相同时;
if(thing[j].w==thing[j+1].w)
{
if(thing[j].price<thing[j+1].price)
{
t=thing[j];
thing[j]=thing[j+1];
thing[j+1]=t;
}
}
}
}
//输出;
for(i=0;i<n;i++)
printf("%d %d\n",thing[i].w,thing[i].price);
}
return 0;
}