微软2017预科生笔试题第一题

微软2017预科生笔试题第一题AC代码:

#include <vector>
#include <math.h>
#include <iostream>
#include <iomanip>

using namespace std;

double pm(int P,double Q, long N, int aa){
    if(N == 0)
        return 0;
    double p_org;
    if(N-1 > aa)
        p_org = 0;
    else{
        int a = floor(P/pow(2,N-1));
        p_org = double(a)/100.0;
    }
    vector<double> ans;
    double tmp;
    if(p_org > 0){
        ans.push_back((1+pm(P,Q,N-1,aa))*p_org);
        tmp = (ans[0]/p_org)-1.0;
    }
    else{
        ans.push_back(0.0);
        tmp = pm(P,Q,N-1,aa);
    }
    double p = p_org;
    double p_b = 1;
    for(int n=1;n<100;n++){
        ans.push_back(n+1+tmp);
        ans[n] = ans[n] * p_b * (1-p);
        if(p+Q>=1){
            double an = 0;
            for(int i=0;i<ans.size();i++){
                an = an+ans[i];
            }
            return an;
        }
        else
        {
            ans[n] = ans[n]*(p+Q);
        }
        p_b = p_b * (1-p);
        p = p + Q;
    }
    return -1;
}

int main(){
    int P,Q,N;
    cin >> P >> Q >> N;
    long maxx = N;
    for(int i=0;i<N;i++){
        if(pow(2,i) >= P){
            maxx = i;
            break;
        }
    }
    double ans = pm(P,Q/100.0,N,maxx);
    cout << setprecision(2)<< setiosflags(ios::fixed) << ans << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值