UVA - 11291 Smeech (模拟)

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) and
e1 and e2 are Smeech expressions. The value represented by a Smeech expression is as follows:

  • An integer represents itself
  • With probability p, (pe1e2) 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 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.00
3.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;
}

时间: 2024-10-27 08:25:52

UVA - 11291 Smeech (模拟)的相关文章

UVA 11291 - Smeech(概率+词法分析)

UVA 11291 - Smeech 题目链接 题意:给定一个表达式形如e=(p,e1,e2) 该表达式的值为 p?(e1+e2)+(1?p)?(e1?e2),求出值 思路:题目是很水,但是处理起来还挺麻烦的,模拟写编译器LEX分析器原理去写了. 代码: #include <cstdio> #include <cstring> const int N = 100005; char str[N]; int now, len, token; double value; void get

UVA 11291 Smeech

[来源]https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2266 此题比较简单,主要工作在于字符串的分割,怎么把代码写得漂亮点才是重点,可读性很重要.利用函数atof(stdlib.h)要比一位位去拼数字要方便点. #include <iostream> #include <stdio.h> #include <

UVA 712(二叉树模拟)

L - S-Trees Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-04-01) Description  S-Trees  A Strange Tree (S-tree) over the variable set  is a binary tree representing a B

UVA 246 - 10-20-30 (模拟+STL)

UVA 246 - 10-20-30 题目链接 题意:给52张的扑克堆,先从左往右发7张牌,之后连续不断从左往右发7张牌,如果有牌堆形成了以下3种情况(按顺序判断): 1.头两张+尾一张和为10或20或30 2.头一张+尾两张和为10或20或30 3.尾三张和为10或20或30 就把这三张牌拿走,放到总牌堆底(这步要不断执行直到不再满足条件或牌堆没了) 如果有一个牌堆因为这个操作被取完了,那么以后将不在这个位置发牌. 如果最后7个牌堆都可以消掉,那么赢,总牌堆用完,那么输,否则平(即不断循环)

UVa 11988 (数组模拟链表) Broken Keyboard (a.k.a. Beiju Text)

题意: 模拟一个文本编辑器,可以输入字母数字下划线,如果遇到'['则认为是Home键,如果是']'则认作End键. 问最终屏幕上显示的结果是什么字符串. 分析: 如果在数组用大量的移动字符必然很耗时.所以next数组表示显示屏中s[i]右边的字符编号,变量cur模拟光标,即当前光标位于s[cur]的右边. 变量last记录显示屏最后一个字符的下标. 我理解的连接的情况应该是这样子的: 1 //#define LOCAL 2 #include <cstdio> 3 #include <cs

UVa 514 Rails(模拟栈)

题意  n辆火车按顺序依次进站  判断给定的出战顺序是否可能 用数组模拟模拟栈代表车站  车依次进站  每当栈顶火车序号与当前要出站的b[cur] 相等时 就让栈顶元素出栈  即top-- #include<cstdio> #include<cstring> using namespace std; const int N = 2000; int b[N], c[N]; int main() { int l, cur, top; while(scanf("%d"

UVa 1589 Xiangqi(模拟 HDU4121)

题意  给你一个黑方被将军的象棋残局  判断红方是否已经把黑方将死 模拟题  注意细节就行了  看黑方的将是否四个方向都不能走 #include<cstdio> #include<cstring> using namespace std; const int N = 12; char brd[N][N]; int dx[] = { -1, 1, 0, 0}, dy[] = {0, 0, -1, 1}; int hx[] = { -2, -1, -2, -1, 1, 2, 1, 2}

UVa 11988 数组模拟链表

题目:在一个没有显示器的电脑上输入一个字符串,键盘坏掉了,会随机的出现home,和end按键, 字符串中'['代表home键(句首),']'代表end键(句尾),问最后输出的字符串的格式. 分析:模拟屏幕操作,移动光标,模拟缓冲区输出操作. 说明:数组模拟链表操作,跟随链表操作,形象化模拟. 1 // UVa11988 Broken Keyboard 2 // Rujia Liu 3 #include<cstdio> 4 #include<cstring> 5 const int

UVA 1589:Xiangqi (模拟 Grade D)

题目: 象棋,黑棋只有将,红棋有帅车马炮.问是否死将. 思路: 对方将四个方向走一步,看看会不会被吃. 代码: 很难看……WA了很多发,还越界等等. #include <cstdio> #include <cstring> #include <cstdlib> char graph[13][13]; int go[4][2] = {{1,0},{0,1},{-1,0},{0,-1}}; bool inBlackPalace(int x, int y) { return