Programming Contest: Etch
Description
Find t (accurate to two decimal places) given the following formula:
[ complete description ] [ etch.java ]
Sample Input
5
10 1000 .001 .001 1
500 1000 .001 .001 1
1000 2000 .00025 .0005 .6931472
40 60 .000001 .0000005 .0003
40 60 .00001 .5 .00001
Sample Output
Crystal #1: 98.00
Crystal #2: 0.57
Crystal #3: 1.00
Crystal #4: 8332.87
Crystal #5: 556.07
Solution
public static final double EPSILON = 0.0000000001;
public void go() throws Exception
{
Scanner in = new Scanner(new File("etch.in"));
int n = in.nextInt();
in.nextLine();
for(int i=0; i<n; i++)
{
String line = in.nextLine();
String sparts[] = line.split(" ");
double parts[] = new double[sparts.length];
for(int j=0; j<5; j++)
{
parts[j] = Double.parseDouble(sparts[j]);
}
double f = (parts[1]-parts[0])/(parts[0]*parts[1]);
double a = parts[2], b = parts[3], c = parts[4];
double low = 0.0, high = 1000000.0;
double t = 0.0, result = 0.0;
while(low < high && Math.abs(f-result) > EPSILON)
{
t = (low+high)/2.0;
result = a*t + b*(1-Math.exp(-1*c*t));
if(f<result)
{
high = t;
}
else if(f==result)
{
break;
}
else //if(f>result)
{
low = t;
}
}
System.out.println("Crystal #"+(i+1)+": "+String.format("%.2f", t)+"n");
}
}
