小朋友学OJ 1063:精挑细选
2018-07-20 本文已影响22人
海天一树X
题目:
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群:658291732
TopCoder & Codeforces & AtCoder交流QQ群:648202993
更多内容请关注微信公众号
wechat_public_header.jpg