首页 > 专题系列 > Java解POJ > POJ 2613 Choose and divide [解题报告] Java
2013
11-11

POJ 2613 Choose and divide [解题报告] Java

Choose and divide

问题描述 :

The binomial coefficient C(m,n) is defined as
            m!

C(m,n) = --------
n!(m-n)!

Given four natural numbers p, q, r, and s, compute the the result of dividing C(p,q) by C(r,s).

输入:

Input consists of a sequence of lines. Each line contains four non-negative integer numbers giving values for p, q, r, and s, respectively, separated by a single space. All the numbers will be smaller than 10,000 with p>=q and r>=s.

输出:

For each line of input, print a single line containing a real number with 5 digits of precision in the fraction, giving the number as described above. You may assume the result is not greater than 100,000,000.

样例输入:

10 5 14 9
93 45 84 59
145 95 143 92
995 487 996 488
2000 1000 1999 999
9998 4999 9996 4998

样例输出:

0.12587
505606.46055
1.28223
0.48996
2.00000
3.99960

解题代码:

/* @author: */
import java.util.Scanner;
public class Main{

 public static void main(String args[]){
  Scanner sc=new Scanner(System.in);  

  int a[]=new int[3],b[]=new int[3],c[]=new int[3],d[]=new int[3],p,q,r,s;
  int maxn;
  double sum;

    int i,flag;
    maxn=100000001;
    while(sc.hasNext())
    {
        p=sc.nextInt();
        q=sc.nextInt();
        r=sc.nextInt();
        s=sc.nextInt();

        a[0]=p;a[1]=s;a[2]=r-s;b[0]=r;b[1]=q;b[2]=p-q;
        for(i=0;i< 3;i++)c[i]=d[i]=2;
        sum=1;flag=1;
        while(flag!=0)
        {
            flag=0;
            for(i=0;i< 3;i++)
            {
                if(sum< maxn&&c[i]<=a[i])
                {
                    flag=1;
                    sum*=c[i]++;
                }    
                if(sum>0.00000001&&d[i]<=b[i])
                {
                    flag=1;
                    sum/=d[i]++;
                }    
            }    
        }    
        System.out.printf("%.5f\n",sum);
    }    
   }
}