poj 1684 Lazy Math Instructor(字符串)
题目链接:http://poj.org/problem?id=1686
思路分析:该问题为表达式求值问题,对于字母使用浮点数替换即可,因为输入中的数字只能是单个digit。
代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std; const int MAX_N = ;
const double factor = 1.1;
double number[MAX_N];
char str[MAX_N];
int len, pos; double Expression( );
double Term( );
double Factor( ); void InputExpression( )
{
int i = ;
pos = ;
string str_in;
getline(cin, str_in); for (i = , len = ; i < str_in.length( ); ++i, ++len)
str[len] = str_in[i];
str[len] = '\0';
} char GetCurrentToken( )
{
while (str[pos] == ' ' || str[pos] == '\t')
pos++;
return str[pos];
} char GetNextToken( )
{
++pos;
while (str[pos] == ' ' || str[pos] == '\t')
pos++;
if (pos == len)
return '\n';
else
return str[pos];
} double Expression( )
{
char ch;
double term1 = Term( ); while ((ch = GetCurrentToken( )) == '+' || (ch == '-')) {
GetNextToken( );
double term2 = Term( ); if (ch == '+')
term1 += term2;
else
term1 -= term2;
} return term1;
} double Term( )
{
char ch;
double factor_1 = Factor( ); while ((ch = GetCurrentToken( )) == '*') {
GetNextToken( );
double factor_2 = Factor( ); factor_1 *= factor_2;
}
return factor_1;
} double Factor( )
{
double value = 0.0;
char ch = GetCurrentToken( ); if (ch == '(') {
GetNextToken( );
value = Expression( );
GetNextToken( );
} else if (isdigit(ch)) {
value = ch - '';
GetNextToken( );
}
else if (ch == '\n')
value = -;
else {
if (number[ch - 'A'] == )
value = number[ch - 'A'] = (ch - 'A') * 1.1;
else
value = number[ch - 'A'];
GetNextToken( );
}
return value;
} int main( )
{
int case_time; scanf("%d\n", &case_time);
while (case_time--) {
InputExpression( );
double ans_1 = Expression( ); InputExpression( );
double ans_2 = Expression( ); if (abs(ans_1 - ans_2) < 1e-)
printf("YES\n");
else
printf("NO\n");
} return ;
}
poj 1684 Lazy Math Instructor(字符串)的更多相关文章
- 数据结构——POJ 1686 Lazy Math Instructor 栈的应用
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...
- POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑
Problem Description A math instructor is too lazy to grade a question in the exam papers in which st ...
- POJ 1686 Lazy Math Instructor(栈)
原题目网址:http://poj.org/problem?id=1686 题目中文翻译: Description 数学教师懒得在考卷中给一个问题评分,因为这个问题中,学生会为所问的问题提出一个复杂的公 ...
- Lazy Math Instructor
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3721 Accepted: 1290 Description A m ...
- UVALive 2056 Lazy Math Instructor(递归处理嵌套括号)
因为这个题目说明了优先级的规定,所以可以从左到右直接运算,在处理嵌套括号的时候,可以使用递归的方法,给定每一个括号的左右边界,伪代码如下: int Cal(){ if(括号) sum += Cal( ...
- POJ - 2183 Bovine Math Geniuses
“模拟“题,运用哈希,不断地按照一定运算规律对一个结果进行计算,如果重复出现就停止并且输出该数.注意到仔细看题,这种题一定要细心! POJ - 2183 Bovine Math Geniuses Ti ...
- POJ 1743 Musical Theme (字符串HASH+二分)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15900 Accepted: 5494 De ...
- poj 3080 Blue Jeans【字符串处理+ 亮点是:字符串函数的使用】
题目:http://poj.org/problem?id=3080 Sample Input 3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCA ...
- POJ 2389 Bull Math(水~Java -大数相乘)
题目链接:http://poj.org/problem?id=2389 题目大意: 大数相乘. 解题思路: java BigInteger类解决 o.0 AC Code: import java.ma ...
随机推荐
- 如何使代码审查更高效【摘自InfoQ】
代码审查者在审查代码时有非常多的东西需要关注.一个团队需要明确对于自己的项目哪些点是重要的,并不断在审查中就这些点进行检查. 人工审查代码是十分昂贵的,因此尽可能地使用自动化方式进行审查,如:代码 ...
- Android 通过HTTP POST请求互联网数据
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s ...
- Linux网络管理——子网掩码
1. 网络基础 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",&q ...
- Javascript面向对象之创建对象
面向对象的语言具有一个共同的标志,那就是具有“类”的概念,但是在javascript中没有类的概念,在js中将对象定义为“无序属性的集合,其属性可以包含基本值,对象或者函数”,即其将对象看作是一组名值 ...
- ADB几种常见的错误及解决方法
下面列举出几种常见的错误及解决方法. Q1:无效的安装包,安装包已损坏[INSTALL_FAILED_INVALID_APK] A1:请检查安装包是否完整.如果是xpk包,可以通过 手动安装xpk来检 ...
- Ubuntu adb devices 出现??? no permissions 的解决方法
在ubuntu 12.10下运行adb devices出现: List of devices attached ???????????? no permissions 1.用命令: lsusb 以 ...
- window.open打开新页面,并将本页数据用过url传递到打开的页面;需要两个页面;
页面1 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8 ...
- 07.20 html5的适配flexible
<script src="http://g.tbcdn.cn/mtb/lib-flexible/0.3.4/??flexible_css.js,flexible.js"> ...
- 音乐ID3 中 专辑封面解析(APIC帧)
ID3V2 中 APIC 帧标识 专辑封面.前几天 百度 谷歌 都没有找到具体的说明.有点小伤人. 最好参考 Android 中的 id3.cpp 以及一个java 开源 id3 库.找到这里的规格 ...
- HDU 2254 奥运(数论+矩阵)
题目中文的不解释啊. .. 须要注意的就是:离散数学中,有向图的邻接矩阵A表示全部点之间路径长度为1的路径数量,A^n则表示路径长度为n的路径数量.故须要求某两点在(A^t1)~(A^t2)的路径数量 ...