Directory
1 Problem Description
2 Solutions
1 problem description
Problem Description
generates a random integer of n ∈[a,b], outputting the probability of their and x.
Input Format
enter four integers on a line, followed by N,a,b,x, separated by a space.
output Format
The output line contains a decimal place and a probability of x, leaving four decimal places after the decimal point
Sample Input
2 1 3 4
Sample Output
0.3333
data size and conventions
for 50% of data, n≤5.
for 100% of data, n≤100,b≤100.
2 Solutions
The following code in the system to run a score of 90, the fifth group of data can not pass, I use the same version of C code to run (PS: see the article at the end of the reference), but can pass, the specific reasons for failure, there may be the following code reasons, such as a classmate Java version of the code can pass, Also hope to share the code OH ~
The specific code is as follows:
ImportJava.util.Scanner; Public classMain { Public Static intN, a, b, X; Public voidGetResult () {Double[] DP =New Double[n + 1] [x + 1]; for(intj = A;j <= b;j++) dp[1][J] = 1.0/(b-a + 1); for(inti = 2;i <= n;i++) { for(intK = a;k <= b;k++) { for(intj = A;j <= x;j++) { if(j-k >= 0) Dp[i][j]+ = Dp[i-1][j-k]/(b-a + 1); }}} System.out.printf ("%.4f", dp[n][x]); } Public Static voidMain (string[] args) {main test=NewMain (); Scanner in=NewScanner (system.in); N=In.nextint (); A=In.nextint (); b=In.nextint (); X=In.nextint (); Test.getresult (); }}
Resources:
1. algorithm to improve probability calculation
Algorithm Note _155: Algorithm improves probability calculation (Java)