hdoj 2048 神,上帝,老天爷 这题 的感悟

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)- C5*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; }

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/ysh-blog/archive/2012/05/12/2497450.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值