一群人坐在一起,每人猜一个 100 以内的数,谁的数字最接近大家平均数的一半就赢。本题就要求你找出其中的赢家。
输入格式:
输入在第一行给出一个正整数N(≤10
4
)。随后 N 行,每行给出一个玩家的名字(由不超过8个英文字母组成的字符串)和其猜的正整数(≤ 100)。
输出格式:
在一行中顺序输出:大家平均数的一半(只输出整数部分)、赢家的名字,其间以空格分隔。题目保证赢家是唯一的。
输入样例:
7
Bob 35
Amy 28
James 98
Alice 11
Jack 45
Smith 33
Chris 62
输出样例:
22 Amy
解题思路: https://pintia.cn/problem-sets/994805046380707840/problems/994805074646122496
解题代码:
#include<iostream>
using namespace std;
const int N = 10010;
int a[N];
string s[N], aws;
int n, sum;
double x, y1, y = 1000000;
int main()
{
cin>>n;
for(int i = 0; i <= n - 1; i ++)
{
cin >> s[i] >> a[i];
sum += a[i];
}
x = sum * 1.0 / 2 / n;
for(int i = 0; i <= n - 1; i ++)
{
y1 = a[i] - x;
if(y1 <= 0) y1 = -1 * y1;
if(y1 < y)
{
y = y1;
aws = s[i];
}
}
cout << sum / n / 2 << " " << aws;
}