Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.3 kB
4
Indexable
// Point Balance 
package Lesson_8;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Point_Balance_2 {
	static int  N;
	static int position[], M[];
	static double index_F1, index_F2;
	static double value[];
	static double F1,F2;
	
	public static void calculatePoint(){
		// Duyet N diem
		for(int i = 0; i < N-1; i++){
			// Chi so duyet cac diem khi tinh luc
			int start = i;
			int end = i + 1;
			// Chi so de tinh cac gia tri mid
			index_F1 = position[i];
			index_F2 = position[i+1];
			
			double n_mid = 0, c_mid = 0;
			double temp = 0;
			boolean check = true;
			while(index_F1 <= index_F2 && check){
				n_mid = (index_F1+index_F2)/2;
				F1 = F2 = 0;
				for(int k = 0; k <= start; k++){
					F1 += M[k]/((position[k] - n_mid) * (position[k]-n_mid));
					
				}
				
				for(int j = end; j < N; j++){
					F2 += M[j]/((position[j] - n_mid) * (position[j]- n_mid));

				}
				if(F1 == F2 || Math.abs(n_mid - c_mid) <= 0.00000000001){
					value[i] = n_mid;
					check = false;
					break;
				}
				else{
					if(F1 < F2){
						index_F2 = n_mid;
					}
					else index_F1 = n_mid;
				}
				
				c_mid = n_mid;
			}
			value[i] = n_mid;
		}
		
	}
	
	
	public static void main(String[] args) throws FileNotFoundException{
		System.setIn(new FileInputStream("Point_Balance"));
		Scanner scanner = new Scanner(System.in);
		for(int tc = 1; tc <= 10; tc++){
			N = scanner.nextInt();
			position = new int [N];
			M = new int[N];
			value = new double[N];
			// Vi tri diem
			for(int i = 0; i < N; i++){
				position[i] = scanner.nextInt();
			}
			// Khoi luong diem
			for(int i = 0; i < N; i++){
				M[i] = scanner.nextInt();
			}
			calculatePoint();
			System.out.print("#" + tc +" ");
			for(int i = 0; i < N-1; i++){
				System.out.printf("%.10f ",value[i] );
			}
			System.out.println();
		}
	}

}

2
1 2 1 1
2
1 2 1 1000
2
457 468 333 321
3
1 2 3 1 2 1
4
2 3 5 7 3 2 7 5
5
3 11 12 19 29 542 661 450 521 366	
6
42 75 88 94 113 144 669 551 355 344 294 155
7
62 86 279 323 363 516 579 810 749 736 297 136 107 52
8
10 34 64 73 93 97 101 122 466 463 441 373 315 292 225 83
10
9 14 38 39 48 73 179 190 207 302 560 497 640 722 437 259 449 470 709 520
Leave a Comment