Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.7 kB
10
Indexable
Never
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class PointofBalance2 {
	static int n;
	static int x1, x2, m1, m2;
	static int a[], m[];
	static int sum;
	static double middle;
	static double result;

	public static void main(String[] args) {
		try {
			System.setIn(new FileInputStream("diemCanBang"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		Scanner sc = new Scanner(System.in);
		for (int tc = 1; tc <= 10; tc++) {
			n = sc.nextInt();
			a = new int[n + 1];
			m = new int[n + 1];
			for (int i = 1; i <= n; i++)
				// vat lieu tu tinh
				a[i] = sc.nextInt();
			for (int i = 1; i <= n; i++)
				// khoi luong vat lieu tu tinh
				m[i] = sc.nextInt();

			System.out.print("#" + tc + " ");
			for (int i = 1; i < n; i++) {
				result = find(i, n, a[i], a[i + 1]);
				System.out.printf("%.10f", result);
				System.out.print(" ");
			}
			System.out.println();

		}
	}

//
//	private static int compareLucHapDan(int left, int right, double toaDoLeft,
//			double toaDoRight) {
//		double lucTrai = 0;
//		double lucPhai = 0;
//		for (int i = 1; i <= left; i++)
//			lucTrai += m[i] / ((middle - a[i]) * (middle - a[i]));
//
//		for (int i = left + 1; i <= right; i++)
//			lucPhai += m[i] / ((a[i] - middle) * (a[i] - middle));
//		//double compare = lucTrai - lucPhai;
//		if (Math.abs(lucTrai - lucPhai) <= 1e-9)
//			return 1;
//		else if (lucTrai>lucPhai) { // xet phai
//			return 2;
//		} else {
//			return 0;
//		}
//
//	}

	private static double find(int left, int right, double toaDoLeft, double toaDoRight) {
//		if (left < right) {
//			middle = (toaDoLeft + toaDoRight) / 2;
//			if (compareLucHapDan(left, right, toaDoLeft, toaDoRight) == 1) {
//				return middle;
//			}
//			else if (compareLucHapDan(left, right, toaDoLeft, toaDoRight) == 0) {
//				return find(left, right, toaDoLeft, middle);
//			}
//
//			else if (compareLucHapDan(left, right, toaDoLeft, toaDoRight) == 2) {
//				return find(left, right, middle, toaDoRight);
//			}
//		}
		
		middle = (toaDoLeft + toaDoRight) / 2;
		
		double lucTrai = 0;
		double lucPhai = 0;
		for (int i = 1; i <= left; i++)
			lucTrai += m[i] / ((middle - a[i]) * (middle - a[i]));

		for (int i = left + 1; i <= right; i++)
			lucPhai += m[i] / ((a[i] - middle) * (a[i] - middle));
		
		if (Math.abs(lucTrai - lucPhai) <= 1e-9) {
			return middle;
		} else {
			if (lucTrai > lucPhai) { // xet phai)
				return find(left, right, middle, toaDoRight);
			} else {
				return find(left, right, toaDoLeft, middle);
			}
		}
		//return lucPhai;
		
		
		//return -1;
	}

}