This is my first time to use c++ to solve the hdoj problem . from this experience , I learned how to control precision of the float . this is too important to me. As a member of my team,I am the only player always using c language to solve problems. Maybe now I need to change my role to become a C++ coder.
here is my code and algorithm to this problem.
algorithm : this is one step connect with another step.It 's seems like recursion to solce this problem.
we need use the former answer(which we have calculated or known) to solve this problem.
for an example :
we calculate the posibility of 5 .
we use s(n) to represent the answer of n.
all the cases is A55 .this is as the Denominator;
and the numerator is A55 - C51*s(4) - C52 *s(3)- C53 *s(2)-1.
hint: remember to "四舍五入";
you need to plus 0.0005 to achieve the precision.
here is the code:
#include<iostream> #include<iomanip> // the head file to achieve precision using namespace std; double a[21],b[21]; double fun(int i); int main() { int i,n; double result; a[2]=1;b[2]=2; //a[n] represent the numerator b[n] represent the denominator a[3]=2;b[3]=6; for(i=4;i<=20;i++) a[i]=fun(i); cin>>n; while(n--) { cin>>i; { result=a[i]/b[i]*100+0.0005; // pay attention to the precision. cout<<setiosflags(ios::fixed)<<setprecision(2)<<result<<"%"<<endl; //setiosflags(ios::fixed) to put answers in float.
// setprecision(2) to put the answers in precision of two. } } } double fun(int n) { double sum=1,son=1,mother=1,result; int i,j,k; for(i=1;i<=n;i++) sum*=i; b[n]=sum; for(j=1;j<=n-2;j++) { son=1; mother=1; for(i=1;i<=j;i++) // this code is to calculate Ckj . mother*=i; for(k=n;k>=n-j+1;k--) son*=k; result=son/mother; //cout<<result*a[n-j]<<endl; sum-=result*a[n-j]; //here is to calculate the numerator. // cout<<sum<<endl; } return sum-1; }