编程介的小学生 2017-09-02 01:52 采纳率: 20.5%
浏览 888
已采纳

Firefighters

A mathematician kept all his documents in a file cabinet near his desk. One day a fire set out in his office and most of his works were badly damaged. Luckily, some of the equations he had solved so many years during his long career were partially preserved. Each equation contained an expression in left side and result in the right. The preserved expressions consisted of all the numbers and brackets, but unfortunately, some of the operators between them were lost in the fire. Another problem was that the results of the equations were scattered and the mathematician was not sure if a certain answer is the result of a certain expression. Your task is to help the mathematician determine if the expressions and the results he was able to save from the fire match one another.
In order to do this, you are given an expression, containing the integer numbers between 1 and 999, simple mathematical operators (+, -, *, /), brackets, and question marks (?), representing the lost mathematical operators. For every expression given, your only task is to state if an expression can or cannot give the required result. In order to help you, the mathematician has chosen only expressions that have the following restrictions:

1.The expressions contain no more than 100 symbols;

2.The brackets enclose no more than 1 operator with his two operands. However, every one of these operands can be an expression in brackets;

3.The constants in the expressions have no sign, i.e. there are no negative numbers in expressions;

4.The maximum number of question marks in the expressions (the lost operators) is less or equal to 10.

The calculation should be performed using the following rules:

  1. The operators * and / are of higher priority than the operators + and -. Parentheses may change the priorities as usually;

  2. The operators +, -, *, and / are left associative, meaning that they group from left to right. If a, b and c are numbers:
    a*b*c = (a*b)*c, a/b/c = (a/b)/c, a/b*c = (a/b)*c, a+b+c = (a+b)+c, a-b+c = (a-b)+c, etc.

  3. When dividing two integers, you should ignore the decimal fraction, for example consider the following equations: 2/5=0, 9/5=1, 100/6=16.

Input: The first line of the input file contains an integer N - determining the number of equations. Next 2*N lines contain the equations. One equation is defined in two lines. The first line is the expression, defining the left side of the equation; second line is an integer result, defining the right side of the equation. The input lines do not have blanks. The strings representing expressions are guaranteed to have no syntax errors.

Output: For every equation in the input file, write yes or no on separate lines on the standard output. If the expression can give the result, write yes. Otherwise, if the result cannot be achieved, write no.

Sample Input

3
1?((2*(3*4))+(5+6))
35
1?2*3+4-14
0
1?3*4/5*6+12
11

Sample Output

yes
no
no

  • 写回答

1条回答

  • threenewbee 2017-09-16 16:25
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?