因为这个题目说明了优先级的规定,所以可以从左到右直接运算,在处理嵌套括号的时候,可以使用递归的方法,给定每一个括号的左右边界,伪代码如下:

int Cal(){

if(括号)  sum += Cal();

   else sum += num;

  return sum;

}

  但是这个题目着实坑了我一下,见过WA了,没见过TLE呢……我因为没有看到有空格这个条件,无线TLE,又是消除函数又是改用数组模拟栈,其实就是读入出错和忘记了处理空格,改了之后,成功AC了。代码如下:

  1. #include<iostream>
  2. #include<cmath>
  3. #include<vector>
  4. #include<algorithm>
  5. #include<cstdio>
  6. #include<cstring>
  7. #include<stack>
  8. using namespace std;
  9. #define maxn 100
  10. int pos[maxn],st[maxn];
  11. int Cal(int id,const char *a){
  12. int sum = ,tmp;
  13. for(int i = id+;i < pos[id];i++){
  14. if(a[i] == '('){
  15. tmp = Cal(i,a);
  16. if(a[i-]=='(' || a[i-]=='+') sum += tmp;
  17. else if(a[i-]=='-') sum -= tmp;
  18. else if(a[i-]=='*') sum *= tmp;
  19. i = pos[i];
  20. }
  21. else if((a[i]>='a'&&a[i]<='z')||(a[i]>=''&&a[i]<='')){
  22. if(a[i]>='a'&&a[i]<='z') tmp = (a[i]-'a'+);
  23. else tmp = a[i]-'';
  24. if(a[i-]=='(' || a[i-]=='+') sum += tmp;
  25. else if(a[i-]=='-') sum -= tmp;
  26. else if(a[i-]=='*') sum *= tmp;
  27. i++;
  28. }
  29. }
  30. return sum;
  31. }
  32. int getnum(const char *a){
  33. memset(pos,,sizeof(pos));
  34. memset(st,,sizeof(st));
  35. int len = strlen(a);
  36. int top = ;
  37. for(int i = ;i < len;i++)
  38. {
  39. if(a[i]=='(') st[top++] = i;
  40. if(a[i]==')') {
  41. pos[st[--top]] = i;
  42. }
  43. }
  44. int sum = ,tmp;
  45. for(int i = ;i < len;i++){
  46. if(a[i]=='('){
  47. tmp = Cal(i,a);
  48. if(i == || a[i-]=='+')
  49. sum += tmp;
  50. else if(a[i-] == '-') sum -= tmp;
  51. else if(a[i-] == '*') sum *= tmp;
  52. i = pos[i];
  53. }
  54. else if((a[i]>='a'&&a[i]<='z')||(a[i]>=''&&a[i]<='')){
  55. if(a[i]>='a'&&a[i]<='z') tmp = (a[i]-'a'+);
  56. else tmp = a[i]-'';
  57. if(i == || a[i-]=='+')
  58. sum += tmp;
  59. else if(a[i-] == '-') sum -= tmp;
  60. else sum *= tmp;
  61. i++;
  62. }
  63. }
  64. //printf("the num = %d\n",sum);
  65. return sum;
  66. }
  67. int main()
  68. {
  69. int t,lena,lenb,num1,num2,tot;
  70. char a[maxn],b[maxn];
  71. scanf("%d",&t);
  72. getchar();
  73. while(t--){
  74. gets(a);
  75. lena = strlen(a);
  76. tot = ;
  77. for(int i = ;i < lena;i++){
  78. if(a[i]==' ') continue;
  79. else b[tot++] = a[i];
  80. }
  81. b[tot] = '\0';
  82. num1 = getnum(b);
  83. gets(a);
  84. lena = strlen(a);
  85. tot = ;
  86. for(int i = ;i < lena;i++){
  87. if(a[i]==' ') continue;
  88. else b[tot++] = a[i];
  89. }
  90. b[tot] = '\0';
  91. num2 = getnum(b);
  92. if(num1 == num2) puts("YES");
  93. else puts("NO");
  94. }
  95. return ;
  96. }

UVALive 2056 Lazy Math Instructor(递归处理嵌套括号)的更多相关文章

  1. 数据结构——POJ 1686 Lazy Math Instructor 栈的应用

    Description A math instructor is too lazy to grade a question in the exam papers in which students a ...

  2. POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑

    Problem Description A math instructor is too lazy to grade a question in the exam papers in which st ...

  3. Lazy Math Instructor

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3721   Accepted: 1290 Description A m ...

  4. poj 1684 Lazy Math Instructor(字符串)

    题目链接:http://poj.org/problem?id=1686 思路分析:该问题为表达式求值问题,对于字母使用浮点数替换即可,因为输入中的数字只能是单个digit. 代码如下: #includ ...

  5. POJ 1686 Lazy Math Instructor(栈)

    原题目网址:http://poj.org/problem?id=1686 题目中文翻译: Description 数学教师懒得在考卷中给一个问题评分,因为这个问题中,学生会为所问的问题提出一个复杂的公 ...

  6. 递归遍历嵌套结构(多层List)中的元素 ------Python

    读Python基础教程(第二版)后看到了这么一个东西,就是利用递归遍历嵌套结构中的元素. 上代码: #encoding:UTF-8 def flatten(nested): try: #不要迭代类似字 ...

  7. python 递归展开嵌套的序列(生成器用法)

    任何使用yield语句的函数都称为生成器.调用生成器函数将创建一个对象,该对象通过连续调用next()方法(在python3中是__next__())生成结果序列. next()调用使生成器函数一直运 ...

  8. Python之旅Day3 文件操作 函数(递归|匿名|嵌套|高阶)函数式编程 内置方法

    知识回顾 常见五大数据类型分类小结:数字.字符串.列表.元组.字典 按存值个数区分:容器类型(列表.字典.元组) 标量原子(数字.字符串) 按是否可变区分:可变(列表.字典) 不可变(数字.字符串.元 ...

  9. 递归、嵌套for循环、map集合方式实现树形结构菜单列表查询

    有时候, 我们需要用到菜单列表,但是怎么样去实现一个菜单列表的编写呢,这是一重要的问题. 比如我们需要编写一个树形结构的菜单,那么我们可以使用JQuery的zTree插件:http://www.tre ...

随机推荐

  1. UVALive 6672 Bonus Cards 概率dp

    题意呢 就是有两种售票方式 一种是icpc 一种是其他方式 icpc抢票成功的概率是其他方式的2倍…… 这时 一个人出现了 他通过内幕知道了两种抢票方式各有多少人 他想知道自己如果用icpc抢票成功的 ...

  2. 利用POI获取Excel中图片和图片位置

    利用POI获取Excel中图片和图片位置(支持excel2003or2007多sheet) 转自:http://blog.csdn.net/delongcpp/article/details/8833 ...

  3. Hanoi汉诺塔问题——递归与函数自调用算法

    题目描述 Description 有N个圆盘,依半径大小(半径都不同),自下而上套在A柱上,每次只允许移动最上面一个盘子到另外的柱子上去(除A柱外,还有B柱和C柱,开始时这两个柱子上无盘子),但绝不允 ...

  4. Openjudge-计算概论(A)-求分数序列和

    描述: 有一个分数序列 2/1,3/2,5/3,8/5,13/8,21/13,.... 求这个分数序列的前n项之和.输入输入有一行:正整数n.输出输出有一行:分数序列的和(浮点数,精确到小数点后4位) ...

  5. MongoDB执行计划分析详解

    要保证数据库处于高效.稳定的状态,除了良好的硬件基础.高效高可用的数据库架构.贴合业务的数据模型之外,高效的查询语句也是不可少的.那么,如何查看并判断我们的执行计划呢?我们今天就来谈论下MongoDB ...

  6. Value '0000-00-00' can not be represented as java.sql.Date

    Value '0000-00-00' can not be represented as java.sql.Date 时间 2014-07-30 09:00:50 ITeye-博客 原文  http: ...

  7. arguments对象,caller 和 callee

    arguments对象是比较特别的一个对象,arguments非常类似Array,但实际上又不是一个Array实例. 它指的是函数对象里的参数,且只能在函数内部使用. 使用 检测函数的参数个数,引用属 ...

  8. C# 获取字符的Unicode编码

    using UnityEngine;using System.Collections;using System.Collections.Generic; List<); string chars ...

  9. 笔记整理--Linux_Socket

    揭开Socket编程的面纱 - 博客 - 伯乐在线 - Google Chrome (2013/9/22 19:28:24) 揭开Socket编程的面纱 2013/09/21 | 分类: IT技术 | ...

  10. C语言-字符串文本串联

    要形成包含多个行的字符串,可以串联两个字符串. 为此,请键入正斜杠,然后按 return 键. 反斜杠导致编译器忽略以下换行符. 例如,字符串     "Long strings can b ...