首页 > ACM题库 > HDU-杭电 > HDU 3347-Calculate the expression-模拟-[解题报告]HOJ
2014
03-16

HDU 3347-Calculate the expression-模拟-[解题报告]HOJ

Calculate the expression

问题描述 :

You may find it’s easy to calculate the expression such as:
a = 3
b = 4
c = 5
a + b + c = ?
Isn’t it?

输入:

The first line contains an integer stands for the number of test cases.
Each test case start with an integer n stands for n expressions will follow for this case.
Then n � 1 expressions in the format: [variable name][space][=][space][integer] will follow.
You may suppose the variable name will only contain lowercase letters and the length will not exceed 20, and the integer will between -65536 and 65536.
The last line will contain the expression you need to work out.
In the format: [variable name| integer][space][+|-][space][variable name| integer] …= ?
You may suppose the variable name must have been defined in the n � 1 expression and the integer is also between -65536 and 65536.
You can get more information from the sample.

输出:

The first line contains an integer stands for the number of test cases.
Each test case start with an integer n stands for n expressions will follow for this case.
Then n � 1 expressions in the format: [variable name][space][=][space][integer] will follow.
You may suppose the variable name will only contain lowercase letters and the length will not exceed 20, and the integer will between -65536 and 65536.
The last line will contain the expression you need to work out.
In the format: [variable name| integer][space][+|-][space][variable name| integer] …= ?
You may suppose the variable name must have been defined in the n � 1 expression and the integer is also between -65536 and 65536.
You can get more information from the sample.

样例输入:

3
4
aa = 1
bb = -1
aa = 2
aa + bb + 11 = ?
1
1 + 1 = ?
1
1 + -1 = ?

样例输出:

12
2
0

点击打开链接

模拟!!

#include"stdio.h"
#include"string.h"
struct node
{
    char str[21];
    int x;
}A[1001];
int main()
{
    int t,n;
    int i,j;
    int cnt,a;
    char s[22];
    scanf("%d",&t);    
    while(t--)
    {
        cnt=0;
        scanf("%d",&n);
        getchar();
        for(i=0;i<n-1;i++)
        {
            scanf("%s = %d",s,&a);
            getchar();
            for(j=0;j<cnt;j++)
            {
                if(strcmp(A[j].str,s)==0)
                {
                    A[j].x=a;break;
                }
            }
            if(j==cnt)
            {
                strcpy(A[cnt].str,s);
                A[cnt].x=a;
                cnt++;
            }
        }
        int ans,f;
        f=ans=0;
        while(1)
        {
            scanf("%s",s);
            if(strcmp("?",s)==0)break;
            if(strcmp("=",s)==0)printf("%d\n",ans);
            else if(strcmp("+",s)==0)f=1;
            else if(strcmp("-",s)==0)f=-1;
            else 
            {
                for(i=0;i<cnt;i++)
                    if(strcmp(A[i].str,s)==0)
                        break;
                if(i==cnt)
                {
                    int t,ff;
                    t=0;ff=1;
                    for(i=0;s[i];i++)
                    {
                        if(s[i]=='-'){ff=-1;continue;}
                        t=t*10+s[i]-'0';
                    }
                    strcpy(A[cnt].str,s);
                    A[cnt].x=t;
                    A[cnt].x*=ff;
                    cnt++;
                    i=cnt-1;
                }
                if(f==0)ans=A[i].x;
                else if(f==1)ans=ans+A[i].x;
                else if(f==-1)ans=ans-A[i].x;
            }
        }
    }
    return 0;
}

参考:http://blog.csdn.net/yangyafeiac/article/details/8797467


  1. 我没看懂题目
    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5
    我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
    第二个是7 0 6 -1 1 -6 7输出是14 7 7
    不知道题目例子是怎么得出来的

  2. 老实说,这种方法就是穷举,复杂度是2^n,之所以能够AC是应为题目的测试数据有问题,要么数据量很小,要么能够得到k == t,否则即使n = 30,也要很久才能得出结果,本人亲测