小朋友学OJ 1063:精挑细选

题目:
http://oj.jzxx.net/problem.php?id=1063

解法一:使用sort函数

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

struct tube     // 结构体名称
{
    int len;    // 长度
    int dia;    // 直径
    int code;   // 编号
}t[1007];       // 数组名称

bool cmp(struct tube a, struct tube b)
{
    if(a.len != b.len)
    {
        return a.len > b.len; // 从大到小
    }
    else
    {
        if(a.dia != b.dia)
        {
            return a.dia < b.dia; // 从小到大
        }
        else
        {
            return a.code > b.code; // 从大到小
        }
    }
}

int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        scanf("%d %d %d",&t[i].len, &t[i].dia, &t[i].code);
    }

    sort(t, t + n, cmp);

    printf("%d\n", t[0].code);

    return 0;
}

解法二:不使用sort函数

#include <stdio.h>
struct stu
{
    int length;
    int width;
    int num;
};

int main()
{
    int m, i, mx = 0;
    struct stu a[1003];
    scanf("%d",&m);
    for(i = 0; i <  m; i++)
    {
        scanf("%d %d %d", &a[i].length, &a[i].width, &a[i].num);
    }
    for(i = 0; i < m; i++)
    {
        if((a[i].length > a[mx].length) ||
           (a[i].length == a[mx].length && a[i].width < a[mx].width) ||
           (a[i].length == a[mx].length && a[i].width == a[mx].width && a[i].num > a[mx].num))
        {
            mx = i;
        }
    }
    
    printf("%d\n", a[mx].num);
    
    return 0;
}

加入少儿信息学奥赛学习QQ群请扫左侧二维码,关注微信公众号请扫右侧二维码

《小朋友学OJ 1063:精挑细选》 QQ群和公众号.png

    原文作者:海天一树X
    原文地址: https://www.jianshu.com/p/d466f1b1634c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞