L1-056 猜数字 (20分)
一群人坐在一起,每人猜一个 100 以内的数,谁的数字最接近大家平均数的一半就赢。本题就要求你找出其中的赢家。
输入格式:
输入在第一行给出一个正整数N(≤)。随后 N 行,每行给出一个玩家的名字(由不超过8个英文字母组成的字符串)和其猜的正整数(≤ 100)。
输出格式:
在一行中顺序输出:大家平均数的一半(只输出整数部分)、赢家的名字,其间以空格分隔。题目保证赢家是唯一的。
输入样例:
7
Bob 35
Amy 28
James 98
Alice 11
Jack 45
Smith 33
Chris 62
输出样例:
22 Amy
这个题感觉还挺简单的,反正赢家是唯一的嘛,那么不管有多少人重复一个数字都和我没有什么关系,反正绝对不是我需要的数据。这样我们直接设置一个100的数组,然后统
计总数据就行了,最后去寻找最近的数字,然后输出名字就可以了。
//#include<bits/stdc++.h> #include <iostream> #include <cstring> #include <string> #include <iomanip> // 格式化输入输出 #include <cmath> #include <cstdlib> #include <vector> #define FUN { for(int i = 0;i < n;i++) cout<<6; } using namespace std; int Function_056( ) { } int main() { string name[101], name_temp; for(int i = 0;i < 101;++i) name[i] = "*"; int n, sum = 0, number; cin>>n; for(int i = 0;i < n;i++) { cin>>name_temp>>number; name[number] = name_temp; sum += number; }
double avg = (double)sum / n / 2; int low,high; low = high = avg;
// 寻找我们需要的名字 while(1) { if(avg - low > high - avg) { if(name[high] != "*") { cout<<(int)avg<<" "<<name[high]<<endl; break; } else ++high; } else { if(name[low] != "*") { cout<<(int)avg<<" "<<name[low]<<endl; break; } else --low; } } return 0; }