题目解析
给定一些人猜的数字 看 谁的数字 最接近大家平均数的一半
输出这个平均数的一半 且输出最靠近这个数的人
解题思路
先求和 取其平均数的一半
再看谁更接近
输出即可
代码
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
public static void main(String[] args) throws IOException
{
int n = ini();
String name[] = new String[n + 10];
int shu[] = new int[n + 10];
int ans = 0;
for (int i = 1; i <= n; i++)
{
name[i] = ins();
shu[i] = ini();
ans += shu[i];
}
double ping = ans * 1.0 / n;
ping /= 2;
double min = 100;
int pos = 0;
for (int i = 1; i <= n; i++)
{
double x = Math.abs(shu[i] - ping);
if (min > x)
{
min = x;
pos = i;
}
}
out.println((int) ping + " " + name[pos]);
out.flush();
out.close();
}
static StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
static int ini() throws IOException
{
sc.nextToken();
return (int) sc.nval;
}
static String ins() throws IOException
{
sc.nextToken();
return sc.sval;
}
}