Algorithm Note _155: Algorithm improves probability calculation (Java)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.