搜狗笔试 Java大题 为什么这个Java代码永远是返回RE啊,换成C++就对了,有大神看看吗
发布于 2017-09-08 18:12 2199 次浏览 0 赞 最后一次编辑是 2017-09-08 18:13 来自 我要提问  
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.PrintWriter;

import java.util.Scanner;

public class Main {

	static final class Solver {
		static final int SIZE = 200005;

		double[] a = new double[SIZE];

		static int n;

		public final void solve(InputReader in, PrintWriter out) {
			while (in.hasNext()) {
				n = in.nextInt();
				for (int i = 0; i < n; ++i) {
					a[i] = in.nextDouble();
				}
				// out.printf("%.8f\n", force());
				out.printf("%.8f\n", ac());
			}
		}

		double ac() {
			double tmp, l, r, res = 0;
			int index;
			for (int i = 0; i < n; ++i) {
				tmp = (a[i] >= 180) ? (a[i] - 180) : (a[i] + 180);
				index = upperBound(0, n - 1, tmp);
				res = Math.max(res, dis(a[(index - 1 + n) % n], a[i]));
				res = Math.max(res, dis(a[index % n], a[i]));
			}
			return res;
		}

		int upperBound(int l, int r, double v) {
			while (l < r) {
				int m = l + (r - l) / 2;
				if (a[m] <= v)
					l = m + 1;
				else if (a[m] > v)
					r = m;
			}
			return l;
		}

		double force() {
			double res = 0.0;
			for (int i = 0; i < n; ++i) {
				for (int j = 0; j < n; ++j) {
					res = Math.max(res, dis(a[i], a[j]));
				}
			}
			return res;
		}


		double dis(double d1, double d2) {
			double res = (d1 > d2) ? (d1 - d2) : (d2 - d1);
			if (res > 180) {
				res = 360 - res;
			}
			return res;
		}
	}

	public static void main(String[] args) {
		PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out, 32768));
		Solver solver = new Solver();
		solver.solve(new InputReader(), out);
		out.close();
	}

	static class InputReader {

		private Scanner reader = new Scanner(new BufferedInputStream(System.in, 32768));

		public int nextInt() {
			try {
				return Integer.parseInt(reader.nextLine());
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			return 0;
		}

		public double nextDouble() {
			try {
				return Double.parseDouble(reader.nextLine());
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			return 0;
		}

		public boolean hasNext() {
			return reader.hasNext();
		}
	}
}


2 条回复

是因为bufferedReader和scanner的原因吗

2017-09-08 18:45

请问你的思路是啥,不想看你代码了。写这么一堆。

2017-09-08 18:46
添加回复
回到顶部