K.猜商品价格游戏

K.猜商品价格游戏
Time Limit: 1000 MSMemory Limit: 32768 K
Total Submit: 33 (20 users)Total Accepted: 19 (19 users)Special Judge: No
Description
提供一件商品,参与者可以估算商品价格,价格最接近的获得最终胜利。

Input
参与者数量 商品实际价格
参与者名字 性别 估算价格
参与者名字 性别 估算价格
参与者名字 性别 估算价格
...
注:参与者数量限定不超过30人,价格为1000以内整数。

Output
胜利者名字 性别 估算价格
胜利者名字 性别 估算价格
...
注:结果为多个胜利者时,输出顺序按照输入的顺序进行输出。

Sample Input
5 500
Nick m 650
Mary f 700
Abby f 450
Mick m 200
Jack m 550

Sample Output
Abby f 450
Jack m 550

Hint
abs函数用于求绝对值,存在于“cmath”头文件中。
使用方法:abs(变量),返回值为变量的绝对值。

#include<iostream>
#include<string>
#include<cmath>
using namespace std;

struct student
{
    string name;
    char sex;
    int price;
};

struct student a[35];

int main() 
{
    int n,m;
    cin >> n >> m;

    for (int i = 0; i < n; i++)
    { 
        cin >> a[i].name >> a[i].sex >> a[i].price;
    }

    int min = abs(a[0].price - m);
    for (int i = 1; i < n; i++)
    {
        if (min > abs(a[i].price-m)) min = abs(a[i].price-m);
    }
    for (int i = 0; i < n; i++)
    {
        if (abs(a[i].price - m) == min)
        {
            cout << a[i].name << ' ' << a[i].sex << ' ' << a[i].price << '\n';
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值