Adding an epsilon is needed because if your answer is very close to zero, small rounding errors could make it negative and make it print as -0.0000. Alternatively, you can print Math.max(0, your answer).
Also, Math.pow(1.0-p,i-1.0) will be NaN for p=1, i=1, and NaN's are never a good thing.
And here:
Code: Select all
try {
while (stdin.ready())
{
String sLine = stdin.readLine();
...
}
} catch (IOException e) {
e.printStackTrace();
}
you shouldn't be catching exceptions or printing any stack traces. Let it be thrown out of main() - then the judge has a chance to say you got "runtime error" instead of potentially confusing "wrong answer".
I'd use this to read the input:
Code: Select all
int T = Integer.parseInt(stdin.readLine().trim()); // <- don't just ignore it!
for (int cs = 1; cs <= T; cs++) {
String sLine = stdin.readLine();
...
}
And here:
Code: Select all
String [] fields = input.split(" +");
if (fields.length != 3) {
I think it's not going to work for leading or trailing whitespace on lines.