sdut oj C语言 结构体 老--质价比

版权声明:本人原创文章若需转载请标明出处和作者!沙沙 https://blog.csdn.net/weixin_44143702/article/details/86517929

老--质价比

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct
{
    int Wi;
    int Pi;
}a[110], t;
int main()
{
    int n, i, j;
    while(~scanf("%d", &n))
    {
        for(i = 0; i < n; i++)
        {
            scanf("%d", &a[i].Wi);
        }
        for(i = 0; i < n; i++)
        {
            scanf("%d", &a[i].Pi);
        }
        for(i = 0; i < n - 1; i++)
        {
            for(j = 0; j < n - 1 - i; j++)
            {
                if(a[j].Wi > a[j + 1].Wi)
                {//按质量升序
                    t = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = t;
                }
                if(a[j].Wi == a[j + 1].Wi)
                {//如果质量相同
                    if(a[j].Pi < a[j + 1].Pi)
                    {//按价格降序
                        t = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = t;
                    }
                }
            }
        }
        for(i = 0; i < n; i++)
        {
            printf("%d %d\n", a[i].Wi, a[i].Pi);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44143702/article/details/86517929