阿里机器学习笔试第一题80% 求大佬指教~
是小明拿奖金的概率的那题,
#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <math.h>
#include<set>
using namespace std;
int main(){
int n;
cin>>n;
vector<double> vec;
double p;
for (int i = 0; i < n; ++i) {
cin>>p;
vec.push_back(p);
}
cout.precision(4);
if(n<=0 || vec[0] <= 0){
cout<<0<<endl;
return 0;
}
double sum = 0;
int innerflag = 0;
double tmp = 1;
for (int j = 0; j < 100; ++j) {
for (int i = 0; i < n; ++i) {
if(1 - innerflag == 1) {
sum += tmp * vec[i];
}
tmp = tmp * (1.0000-vec[i]);
innerflag = 1 - innerflag;
}
if(tmp < 0.00003 || sum >= 0.9999){
break;
}
}
cout.precision(4);
cout<<sum<<endl;
return 0;
}
-----------------更新-----------------
错在没有理解对题意,我的代码是把p1,p2,...,pn当做一轮,但实际题意为每次算一轮,所以要去掉外层那个100次循环,还要加上判停条件~我知道错了,大佬们不要再鞭尸啦! 回复里有大佬们写的正确代码