zoj-3782-Ternary Calculation

来源:互联网 发布:vb 向上取整 编辑:程序博客网 时间:2024/03/29 15:22

Description

Complete the ternary calculation.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

There is a string in the form of "number1operatoranumber2operatorbnumber3". Each operator will be one of {'+', '-' , '*', '/', '%'}, and each number will be an integer in [1, 1000].

Output

For each test case, output the answer.

Sample Input

51 + 2 * 31 - 8 / 31 + 2 - 37 * 8 / 55 - 8 % 3

Sample Output

7-10113

Note

The calculation "A % B" means taking the remainder of A divided by B, and "A / B" means taking the quotient.


总共只有那么几种情况,也就两个操作符三个数,实在不行枚举也行

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int main()  {      int i,j,t,a,b,c,ans;      char o1,o2;      scanf("%d",&t);      while(t--)      {          scanf("%d%*c%c%*c%d%*c%c%*c%d",&a,&o1,&b,&o2,&c);          if((o1=='+'||o1=='-')&&(o2=='*'||o2=='/'||o2=='%'))          {              switch(o2)              {                  case'*':ans=b*c;break;                  case'/':ans=b/c;break;                  case'%':ans=b%c;              }              switch(o1)              {                  case'+':ans+=a;break;                  case'-':ans=a-ans;              }          }          else          {              switch(o1)              {                  case'+':ans=a+b;break;                  case'-':ans=a-b;break;                  case'*':ans=a*b;break;                  case'/':ans=a/b;break;                  case'%':ans=a%b;              }              switch(o2)              {                  case'+':ans+=c;break;                  case'-':ans-=c;break;                  case'*':ans*=c;break;                  case'/':ans/=c;break;                  case'%':ans%=c;              }          }          printf("%d\n",ans);      }      return 0;  }  


0 0