Problem Description
Professor Octastichs has invented a new programming language, Smeech. An expression in Smeech may be a positive or negative integer, or may be of the form (p e1 e2) where p is a real number between 0 and 1 (inclusive) and e1 and e2 are Smeech expressions. The value represented by a Smeech expression is as follows:
1) An integer represents itself
2) With probability p, (p e1 e2) represents x+y where x is the value of e1 and y is the value of e2; otherwise it represents x-y.
Given a Smeech expression, what is its expected value?
1) An integer represents itself
2) With probability p, (p e1 e2) represents x+y where x is the value of e1 and y is the value of e2; otherwise it represents x-y.
Given a Smeech expression, what is its expected value?
Input
Input consists of several Smeech expressions, one per line, followed by a line containing ().
Output
For each expression, output its expected value to two decimal places.
Sample Input
7 (.5 3 9) ()
Sample Output
7.00 3.00
Author
Gordon V. Cormack
Source
Recommend
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
double func(char *c)
{
double d;
sscanf(c,"%lf",&d);
return d;
}
double T(char *b)
{ int t=1;int n=0;
double p,x,y;
if(b[t]!='(')
{
p=func(b+t);
while(b[t]!=' '&&b[t]!=')'&&b[t]!='(')
{
t++;
}
}
if(b[t]=='(')
{
p=T(b+t);
t++;
while(n+1)
{
if(b[t]=='(')
n++;
if(b[t]==')')
n--;
t++;
}
}
n=0;
if(b[t]==' ')
t++;
if(b[t]!='(')
{
x=func(b+t);
while(b[t]!=' '&&b[t]!=')'&&b[t]!='(')
{
t++;
}
}
if(b[t]=='(')
{
x=T(b+t);
t++;
while(n+1)
{
if(b[t]=='(')
n++;
if(b[t]==')')
n--;
t++;
}
}
n=0;
if(b[t]==' ')
t++;
if(b[t]!='(')
{
y=func(b+t);
while(b[t]!=' '&&b[t]!=')'&&b[t]!='(')
{
t++;
}
}
if(b[t]=='(')
{
y=T(b+t);
t++;
while(n+1)
{
if(b[t]=='(')
n++;
if(b[t]==')')
n--;
t++;
}
}
n=0;
return (x+y)*p+(x-y)*(1-p);
}
int main()
{
double a;char b[99999];
while(gets(b))
{
if(b[0]!='(')
{
sscanf(b,"%lf",&a);
printf("%0.2lf\n",a);
}
if(b[0]=='('&&b[1]==')')
break;
else if(b[0]=='(')
printf("%0.2lf\n",T(b));
}
return 0;
}
/*#include<stdio.h>
#include<string.h>
#include<math.h>
char s[1248];
double Getanswer(char *s)
{
double ans,p,x,y;
int i,j,n;
if(s[0]!='(')//输入的就是一个数字
{
sscanf(s,"%lf",&ans);//字符串转换为double
return ans;
}
sscanf(s+1,"%lf",&p);
i=0;
while(s[i++]!=' '); //while(s[i]!=' ') { i++; }
if(s[i]!='(')
{
sscanf(s+i,"%lf",&x);
while(s[i++]!=' ');
if(s[i]!='(') sscanf(s+i,"%lf",&y);
else y=Getanswer(s+i);
}
else
{
n=1;
for(j=i+1;n;j++)//左右括号的析取抵消
{
if(s[j]=='(') ++n;
if(s[j]==')') --n;
}
s[j]='\0';
x=Getanswer(s+i);
if(s[j+1]=='(') y=Getanswer(s+j+1);
else sscanf(s+j+1,"%lf",&y);
}
return x+(p+p-1)*y;
}
int main()
{
while(gets(s))
{
if(strcmp(s,"()")==0) break;
printf("%.2lf\n",Getanswer(s));
}
}
*/
/*
#include<iostream>
#include<cmath>
#include<cstdio>
#include<map>
#include<set>
#include<cstring>
#include<string>
#include<algorithm>
#define min 1e-6
using namespace std;
char s[100000];
int FLAG;
double func1(char c)
{
char str[100000];
str[0]=c;
int flag=1;
while(str[flag++]=s[FLAG++])
if(str[flag-1]==' '||str[flag-1]=='\n'||str[flag-1]==')')
break;
str[flag-1]='\0';
double re;
sscanf(str,"%lf",&re);
return re;
}
double func2()
{
double a[3];
FLAG++;
for(int i=0; i<3; i++)
if(s[FLAG]!=0)
if(s[FLAG]!='(')
a[i]=func1(s[FLAG++]);
else
a[i]=func2();
FLAG++;
return (a[2]+a[1])*a[0]+(a[1]-a[2])*(1-a[0]);
}
int main()
{
while(gets(s)&&strcmp(s,"()"))
{
FLAG=0;
double x;
if(s[0]!='(')
sscanf(s,"%lf",&x);
else
x=func2();
printf("%.2lf\n",x);
}
return 0;
}
*/