I had a problem where, my answers were completely similar to the ones on the online judge, but they were deemed wrong.
I fixed it after a lot of tinkering, but I dont understand what is going on. Please explain what is going wrong.
This was my first program that gave the exact answers as the sample, but the onlinejudge said that it was wrong.
#include <stdio.h>
int main(){
double score,div,prob,coin=0.5;
int n,k;
scanf("%d %d",&n,&k);
div= 1.0/n;
for(int i=1;i<=n;i++){
score =i;
coin=1;
while(score<k){
score*=2;
coin*=0.5;
}
prob= prob+(coin * div);
}
printf("%.12lf",prob);
}
The onlinejudge says that this is correct:
#include <stdio.h>
int main(){
double score,div,prob=0.0,coin=0.5;
int n,k;
scanf("%d %d",&n,&k);
div= 1.0/n;
for(int i=1;i<=n;i++){
score =i;
coin=1;
while(score<k){
score*=2;
coin*=0.5;
}
prob= prob+(coin * div);
}
printf("%.12lf",prob);
}
I only changed the
double prob;
to
double prob=0.0;
Why is the first one wrong?
14