L1-8 猜數字 (20分) java

一羣人坐在一起,每人猜一個 100 以內的數,誰的數字最接近大家平均數的一半就贏。本題就要求你找出其中的贏家。

輸入格式:

輸入在第一行給出一個正整數N(≤104\le 10^4104)。隨後 N 行,每行給出一個玩家的名字(由不超過8個英文字母組成的字符串)和其猜的正整數(≤\le 100)。

輸出格式:

在一行中順序輸出:大家平均數的一半(只輸出整數部分)、贏家的名字,其間以空格分隔。題目保證贏家是唯一的。

輸入樣例:

7
Bob 35
Amy 28
James 98
Alice 11
Jack 45
Smith 33
Chris 62

輸出樣例:

22 Amy

二分查找版,有一個測試點錯誤,沒找到原因

import java.io.IOException;
import java.io.*;
import java.util.*;

//** Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static String nextLine() throws IOException {
		return reader.readLine();
	}
	static char nextChar() throws IOException {
		return next().charAt(0);
	}
	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
}

public class Main {
	static int[]number;
	static boolean flag = true;
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		int n = Reader.nextInt();
		String []name = new String[10001];
		number = new int[n];
		double sum = 0;
		for (int i = 0; i < n; i++) {
			String person_name = Reader.next();
			int guess = Reader.nextInt();
			name[guess] = person_name;
			number[i] = guess;
			sum+=number[i];
		}
		Arrays.sort(number);
		double avg = sum/n/2;
		int index = binarySearch(0,number.length-1,avg);
		if (flag) {
			System.out.println((int)avg+" "+name[number[index]]);
		} else {
			double []t;
			if (index==n-1) {
				t = new double[2];
				t[0] = Math.abs(number[index-1]-avg);
				t[1] = Math.abs(number[index]-avg);
				if (t[0]<t[1]) {
					System.out.println((int)avg+" "+name[number[index-1]]);
				} else {
					System.out.println((int)avg+" "+name[number[index]]);
				}
			} else if(index==0) {
				t = new double[2];
				t[0] = Math.abs(number[index]-avg);
				t[1] = Math.abs(number[index+1]-avg);
				if (t[0]<t[1]) {
					System.out.println((int)avg+" "+name[number[index]]);
				} else {
					System.out.println((int)avg+" "+name[number[index+1]]);
				}
			} else {
				t = new double[3];
				t[0] = Math.abs(number[index-1]-avg);
				t[1] = Math.abs(number[index]-avg);
				t[2] = Math.abs(number[index+1]-avg);
				if (t[0]<t[1]&&t[0]<t[2]) {
					System.out.println((int)avg+" "+name[number[index-1]]);
				} else if(t[1]<t[0]&&t[1]<t[2]) {
					System.out.println((int)avg+" "+name[number[index]]);
				} else if(t[2]<t[1]&&t[2]<t[0]) {
					System.out.println((int)avg+" "+name[number[index+1]]);
				}
			}
		}
		
	}

	private static int binarySearch(int i, int j, double avg) {
		if (i>j) {
			flag = false;
			return j;
		} else {
			int mid = (i+j)/2;
			if (avg>number[mid]) {
				return binarySearch(mid+1, j, avg);
			} else if(avg==number[mid]){
				return mid;
			} else {
				return binarySearch(i, mid-1, avg);
			}
		}
	}
}

這是借鑑了同學的想法,暴力搜,從平均值開始左右搜,搜到第一個就是答案,這裏有個注意的地方就是sum不要聲明爲double,double和int相加比較耗時

import java.io.IOException;
import java.io.*;
import java.util.*;

//** Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static String nextLine() throws IOException {
		return reader.readLine();
	}
	static char nextChar() throws IOException {
		return next().charAt(0);
	}
	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
}

public class Main {
	
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		int n = Reader.nextInt();
		String []name = new String[10001];
		int sum = 0;
		for (int i = 0; i < n; i++) {
			String person_name = Reader.next();
			int guess = Reader.nextInt();
			name[guess] = person_name;
			sum+=guess;
		}
		int avg = (int)(1.0*sum/n/2);
		for (int i = 0; i < name.length; i++) {
			if (name[avg+i]!=null) {
				System.out.println(avg+" "+name[avg+i]);
				break;
			}
			if (name[avg-i]!=null) {
				System.out.println(avg+" "+name[avg-i]);
				break;
			}
		}
	}
}

極限通過,如果沒通過多提交兩次。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章