B 题
输入,只有1d10这种形式时,
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<sstream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
//求期望
double f(double a)
{
return ((a+1)/2);
}
int main()
{
string str;
int flag=0;
double num=0;
getline(cin,str,'\n');
int left=0,right=0;
string subs;
string tc,ts;
int temp;
//
for(int i=0; i<str.size(); ++i)
{
if(str[i]=='+')
{
flag=1;
right=i;
subs=str.substr(left,right-left);
temp=subs.find('d',0);
if(temp==-1)
{
num+=atof(subs.c_str());
}
else
{
tc=subs.substr(0,temp);
ts=subs.substr(temp+1,subs.size()-temp-1);
num+=atof(tc.c_str())*f(atof(ts.c_str()));
}
left=right+1;
}
}
//对最后一个
subs=str.substr(right+1,str.size()-right-1);
temp=subs.find('d',0);
if(temp==-1)
{
num+=atof(subs.c_str());
}
else
{
tc=subs.substr(0,temp);
ts=subs.substr(temp+1,subs.size()-temp-1);
num+=atof(tc.c_str())*f(atof(ts.c_str()));
}
if(flag==0)
num=atof(str.c_str());
if((int)(num*2)%2==0)
printf("%.0lf\n",num);
else
printf("%.1lf\n",num);
return 0;
}
#include<algorithm>
#include<vector>
#include<string>
#include<sstream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
//求期望
double f(double a)
{
return ((a+1)/2);
}
int main()
{
string str;
int flag=0;
double num=0;
getline(cin,str,'\n');
int left=0,right=0;
string subs;
string tc,ts;
int temp;
//
for(int i=0; i<str.size(); ++i)
{
if(str[i]=='+')
{
flag=1;
right=i;
subs=str.substr(left,right-left);
temp=subs.find('d',0);
if(temp==-1)
{
num+=atof(subs.c_str());
}
else
{
tc=subs.substr(0,temp);
ts=subs.substr(temp+1,subs.size()-temp-1);
num+=atof(tc.c_str())*f(atof(ts.c_str()));
}
left=right+1;
}
}
//对最后一个
subs=str.substr(right+1,str.size()-right-1);
temp=subs.find('d',0);
if(temp==-1)
{
num+=atof(subs.c_str());
}
else
{
tc=subs.substr(0,temp);
ts=subs.substr(temp+1,subs.size()-temp-1);
num+=atof(tc.c_str())*f(atof(ts.c_str()));
}
if(flag==0)
num=atof(str.c_str());
if((int)(num*2)%2==0)
printf("%.0lf\n",num);
else
printf("%.1lf\n",num);
return 0;
}
代码通过
全部评论
(0) 回帖