POJ 3645 解题报告
来源:互联网 发布:c语言程序的基本单位 编辑:程序博客网 时间:2024/05/23 00:42
这道题是简单题。就是递归地解析,求期望。
但是我还是用了很久。。。代码也写得很复杂。看到有神人的清晰代码:
https://github.com/wangyongliang/Algorithm/blob/master/poj/3645/POJ_3645_3644037_AC_0MS_196K.cpp
/* ID: thestor1 LANG: C++ TASK: poj3645 */#include <iostream>#include <fstream>#include <cmath>#include <cstdio>#include <cstring>#include <limits>#include <string>#include <vector>#include <list>#include <set>#include <map>#include <queue>#include <stack>#include <algorithm>#include <cassert>using namespace std;double eval(char line[]){int len = strlen(line);// printf("line(%d):[%s]\n", len, line);// strip the wightspaces;while (*line == ' '){line++;len--;}while (line[len - 1] == ' '){len--;}double p, x, y;int n1 = 0, num;// if it is an expressionif (line[0] == '('){// delete the matching ')' in the endassert(line[len - 1] == ')');line[len - 1] = '\0';// delete the first '('line++;char *e;e = line;while (*e != ' '){e++;}*e = '\0';sscanf(line, "%lf", &p);// printf("p:%lf\n", p);e++;// if it is another expressionif (e[0] == '('){int unmatched = 1;for (n1 = 1; e[n1] && unmatched; ++n1){if (e[n1] == '('){unmatched++;}else if (e[n1] == ')'){unmatched--;}}assert(unmatched == 0 && e[n1 - 1] == ')');assert(e[n1] == ' ');e[n1] = '\0';x = eval(e);}else{// it is an integer herewhile (e[n1] != ' '){++n1;}e[n1] = '\0';sscanf(e, "%d", &num);x = num;}// e2 follows e1y = eval(e + n1 + 1);return p * (x + y) + (1 - p) * (x - y);}else{sscanf(line, "%d", &num);x = num;return x;}}int main(){// 7// (.5 3 9)char line[1000];double p;int e1, e2;while (gets(line) && strcmp(line, "()") != 0){printf("%.2lf\n", eval(line));}return 0; }
0 0
- POJ 3645 解题报告
- poj数百篇解题报告
- poj 1141解题报告
- poj 1001解题报告
- poj 1011 解题报告
- poj 1087 解题报告
- poj 2153解题报告
- poj 2051解题报告
- POJ 1167 解题报告
- poj 1026 解题报告
- POJ 1077 解题报告
- POJ 1042 解题报告
- POJ 1095 解题报告
- POJ 1159解题报告
- poj 2312解题报告
- POJ 1001解题报告
- POJ 1002解题报告
- POJ 1006解题报告
- 【cocos2d-x 3.7 飞机大战】 决战南海I (十一) 关于游戏场景
- 【cocos2d-x 3.7 飞机大战】 决战南海I (十二) 游戏结束场景
- 【cocos2d-x 3.7 飞机大战】 决战南海I (十三) 分数场景
- Leecode_Missing number
- Leetcode: Flatten Binary Tree to Linked List
- POJ 3645 解题报告
- 【LeetCode-面试算法经典-Java实现】【198-House Robber(抢劫犯)】
- Spring MVC MultiActionController example
- 【LeetCode-面试算法经典-Java实现】【199-Binary Tree Right Side View(从右边看二叉树)】
- Spring MVC MultiActionController annotation example
- 【LeetCode-面试算法经典-Java实现】【200-Number of Islands(岛的数目)】
- Spring MVC PropertiesMethodNameResolver example
- 正则表达式测试工具 regexbuddy
- HDU 3625 Examining the Rooms(第一类斯特林数)