UVA - 11291 Smeech (模拟)
来源:互联网 发布:java调用本地方法 编辑:程序博客网 时间:2024/06/04 01:17
Description
Problem B: Smeech
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(pe1e2) where p is a real number between 0 and 1 (inclusive) ande1 and e2 are Smeech expressions. The value represented by a Smeech expression is as follows:- An integer represents itself
- With probability p, (pe1e2) representsx+y where x is the value of e1 and y is the value ofe2; otherwise it represents x-y.
Input consists of several Smeech expressions, one per line, followed by a line containing (). For each expression, output its expected value to two decimal places.
Sample Input
7(.5 3 9)()
Output for Sample Input
7.003.00题意:给出个表达式(p,e1,e2)表示(e1+e2)*p+(e1-e2)*(1-p)的结果,求最后的值思路:递归的处理整个式子,注意细节小数的时候的判断#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 10005;char str[maxn];int cur, len;double cal() {double op = 1;while (!(str[cur]=='(' || (str[cur]>='0'&&str[cur]<='9') || str[cur]=='-') && cur < len)cur++;if (str[cur] == '-') {cur++;op = -1;}if (str[cur] == '(') {cur++;double w = 0.1, p = 0;if (str[cur] == '.' || str[cur+1] == '.') {if (str[cur+1] == '.')cur++;for (int i = cur+1; str[i] >= '0' && str[i] <= '9' && str[i]; i++, cur = i) {p = p + (str[i] - '0') * w;w *= 0.1;}}else p = str[cur++] - '0';double a, b;a = cal();b = cal();return (a + b) * p + (a - b) * (1 - p);}else {double p = 0;for (int i = cur; str[i] >= '0' && str[i] <= '9' && str[i]; i++, cur = i)p = p * 10 + str[i] - '0';return op * p;}}int main() {double p, a, b;while (gets(str) && strcmp(str, "()")) {len = strlen(str);cur = 0;printf("%.2lf\n", cal());}return 0;}
1 0
- UVA - 11291 Smeech (模拟)
- UVA 11291 - Smeech(概率+词法分析)
- uva 11291 - Smeech(概率+词法分析)
- Smeech
- UVA Palindromes(模拟)
- uva 133(模拟)
- UVA 12662(模拟)
- UVA 12503(模拟)
- UVA 12504(模拟)
- uva 445 uva 489 uva 490 uva 694(模拟)
- uva 409(字符串模拟)
- UVA 101(栈模拟)
- uva 10152 ShellSort(模拟)
- UVA 305 - Joseph(模拟)
- UVa 230 - Borrowers(模拟)
- UVA Automatic Poetry(模拟)
- UVa 220 - Othello(模拟)
- UVa 1592模拟(map)
- java获取日期
- SQL Server 2008 常用命令
- work_weipa_百度云推送
- 关于linux字符驱动编写接口函数的参数传递问题
- Asmack开发系列之通信数据流
- UVA - 11291 Smeech (模拟)
- 华为服务器被黑,凸显电子邮件加密的重要性
- 智能工坊孙东波:探索智能家居行业细分市场
- 上海商报:超级表格创始人叫板Excel
- iOS 判断 NSUserdefault 是否存在某个 key
- Android Studio 快捷鍵
- Java HashMap的死循环
- weiphp的使用
- 天涯明月刀 题一