java - Collision Detection algorithm -
this algorithm i'm trying solve:
there n circular organisms radii r0,r1,..rn-1 . during each second, 2 organisms radii ri , rj , (chosen randomly , equiprobably) collide form new, bigger organism radius ri+rj. enter image description here
given radii n creatures , integer,k, print single floating-point number denoting expected total area of creatures after k seconds.
input format
the first line contains 2 space-separated integers describing respective values of n , k. second line contains n space-separated integers describing respective values of r0,r1,..rn-1
constraints
1 < n < 10^5
0< k < n-1
1 < ri < 10^4
an answer considered correct if has relative error of @ 10^-9.
output format
print single floating-point number denoting expected sum of creatures' areas after k collisions.
sample input 0
3 1
1 2 3
sample output 0
67.0206432766
sample input 1
3 2
1 2 3
sample output 1
113.0973355292
explanation 1
we have n = 3 organisms radii in {1, 2, 3}. after k = 2 seconds, 2 collisions take
place , we're left 1 circle has radius of 6. print total area taken remaining organism, pi (3.14159265359) * 6^2 = 113.0973355292.
i started writing out code folowing:
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class solution { public static void main(string[] args) { scanner in = new scanner(system.in); int n = in.nextint(); int k = in.nextint(); int[] r = new int[n]; for(int r_i=0; r_i < n; r_i++){ r[r_i] = in.nextint(); } double sum=0; double avrage=0; double pi = 3.1415926535; for(int i=0;i<n;i++){ int sumj=0; for(int j=0;j<n;j++){ if(j!=i) sumj=sumj+r[j]; system.out.println(sumj); } sum=sum+pi*math.pow((r[i]),2)+pi*math.pow(sumj,2); } avrage=sum/n; system.out.println(avrage); } }
the problem i'm not sure should use parameter k(seconds) solve problem. works k=1(after second). should change code work k=2(after 2 seconds) or k number(after x seconds)?
Comments
Post a Comment