Untitled
plain_text
2 months ago
2.1 kB
2
Indexable
Never
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class Pob2 { static{ try { System.setIn(new FileInputStream("src/inp.txt")); // System.setOut(new PrintStream("src/out.txt")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } static double diff=1e-13;//NOTE o day phai la 1e-13ko phai 1e-9 static double search(int[] mass,int pos[],int start,int end,double l,double r) { while(r-l>=diff) { //sai khac giua toa do >= diff (sai so cho phep) double m=l+(r-l)/2d; double f=0; for(int i=0;i<=start;++i) f+=(double)mass[i]/((m-pos[i])*(m-pos[i]));//m la toa do cua vat bi tac dong luc for(int i=end;i<pos.length;++i) f-=(double)mass[i]/((pos[i]-m)*(pos[i]-m)); if(f<diff && f>-diff)//note xem xet ca sai so cua luc tac dong len diem giua, neu < cho phep thi stop break; else if(f>0)//luc ben trai>ben phai thi tang l de giam luc ben trai l=m; else r=m; } return l+(r-l)/2d; } /*tim kiem nhi phan * dau tien xac dinh cong thuc tinh luc tac dung len 1 diem bat ky * F=Gm1m2/d^2 => m1 klg vat tac dung, m2 vat bi tac dong, d: kc giua 2 vat * => nhieu vat tac dong len: m1/d1^2 + m2/d2^2 + ... = mn/dn^2 * => de can bang luc trai va phai: => m1/d1^2 + m2/d2^2 + ... - mn/dn^2 ~ 0 * tim kiem nhi phan theo toa do chieu dai cua tung vat * */ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int t=0; while(scanner.hasNext()) { int n=Integer.parseInt(scanner.next()); int[] pos=new int[n],mass=new int[n]; for(int i=0;i<n;++i) pos[i]=Integer.parseInt(scanner.next()); for(int i=0;i<n;++i) mass[i]=Integer.parseInt(scanner.next()); System.out.printf("#%d ",++t); for(int i=0;i<n-1;++i)//xac dinh n-1 diem can bang cho n diem System.out.printf("%.10f ",search(mass, pos, i, i+1, pos[i], pos[i+1])); System.out.print('\n'); } scanner.close(); } }