Problem description
Guo Jie wants you to help him find the value of F.
Enter a description
Example of multi-group test sample.
A positive n (4<=n<=1e9) output description for each set of samples that is greater than 3
Each set of test sample output one row. (Results reserved 12 decimal places) sample input
4
Sample output
0.250000000000
SOURCE 12th session of the North University program design contest Tips
Hehe, secretly tell you, really white again in test Guo Jie Math ~
Ideas:
If the problem is calculated as equal to 4 and not equal to 4, then it will time out. So the main problem is to find 6/(n (n-1) * (n-2)) this law.
Timeout code:
#include <stdio.h>
#include <stdlib.h>/
* Run this program using the console Pauser or add your own GE TCH, System ("pause") or input loop *
/int main () {
double n, sum = 1;
int i;
while (scanf ("%lf", &n)!=eof) {
double A = n;
if (n = = 4) {
sum = 1.0 * 1.0/4;
printf ("%.12f\n", sum);
} else if (n>4) {for
(i=1;i<n-3;i++) {
sum *= i/a;
a--;
}
printf ("%.12f\n", sum);
}
}
return 0;
}
Correct code:
#include <stdio.h>
#include <stdlib.h>/
* Run this program using the console Pauser or add your own Getch, System ("pause") or input loop *
/int main () {
double n, sum = 0;
while (scanf ("%lf", &n)!=eof) {
sum = 6/(n (n-1) * (n-2));
printf ("%.12f\n", sum);
}
return 0;
}