ZOJ 3782 Ternary Calculation

G - Ternary Calculation
Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu
Submit Status Practice ZOJ 3782

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

5
1 + 2 * 3
1 - 8 / 3
1 + 2 - 3
7 * 8 / 5
5 - 8 % 3

Sample Output

7
-1
0
11
3

Note

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

根据字符串等式写出结果。

我是直接暴力的。代码虽然长了点,反正也不会超时。

优化的话,可以根据运算符的优先级计算。

#include <stdio.h>
#include <string.h>
char s[3005];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int ans;
        int a,b,c;
        char op1,op2;
        scanf("%d%*c%c%*c%d%*c%c%*c%d",&a,&op1,&b,&op2,&c);
        if((op1=='+'||op1=='-')&&(op2=='+'||op2=='-'))
        {
            switch(op1)
            {
                case '+':
                    switch(op2)
                    {
                        case '+':
                            ans=a+b+c;
                            break;
                        case '-':
                            ans=a+b-c;
                            break;
                    }
                    break;
                case '-':
                    switch(op2)
                    {
                        case '+':
                            ans=a-b+c;
                            break;
                        case '-':
                            ans=a-b-c;
                            break;
                    }
                    break;
            }

        }
        if((op1=='+'||op1=='-')&&(op2=='*'||op2=='/'||op2=='%'))
        {
            switch(op1)
            {
                case '+':
                    switch(op2)
                    {
                        case '*':
                            ans=a+b*c;
                            break;
                        case '/':
                            ans=a+b/c;
                            break;
                        case '%':
                            ans=a+b%c;
                            break;
                    }
                    break;
                case '-':
                    switch(op2)
                    {
                        case '*':
                            ans=a-b*c;
                            break;
                        case '/':
                            ans=a-b/c;
                            break;
                        case '%':
                            ans=a-b%c;
                            break;
                    }
                    break;
            }
        }
        if((op1=='*'||op1=='/'||op1=='%')&&(op2=='*'||op2=='/'||op2=='%'))
        {
            switch(op1)
            {
                case '*':
                    switch(op2)
                    {
                        case '*':
                            ans=a*b*c;
                            break;
                        case '/':
                            ans=a*b/c;
                            break;
                        case '%':
                            ans=a*b%c;
                            break;
                    }
                    break;
                case '/':
                    switch(op2)
                    {
                        case '*':
                            ans=a/b*c;
                            break;
                        case '/':
                            ans=a/b/c;
                            break;
                        case '%':
                            ans=a/b%c;
                            break;
                    }
                    break;
                 case '%':
                    switch(op2)
                    {
                        case '*':
                            ans=a%b*c;
                            break;
                        case '/':
                            ans=a%b/c;
                            break;
                        case '%':
                            ans=a%b%c;
                            break;
                    }
                    break;
            }

        }
        if((op1=='*'||op1=='/'||op1=='%')&&(op2=='+'||op2=='-'))
        {
            switch(op1)
            {
                case '*':
                    switch(op2)
                    {
                        case '+':
                            ans=a*b+c;
                            break;
                        case '-':
                            ans=a*b-c;
                            break;
                    }
                    break;
                case '/':
                    switch(op2)
                    {
                        case '+':
                            ans=a/b+c;
                            break;
                        case '-':
                            ans=a/b-c;
                            break;
                    }
                    break;
                 case '%':
                    switch(op2)
                    {
                        case '+':
                            ans=a%b+c;
                            break;
                        case '-':
                            ans=a%b-c;
                            break;
                    }
                    break;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(ZOJ 3782 Ternary Calculation)