zoukankan      html  css  js  c++  java
  • BNU29368:Check the Identity(栈)

    Just determine whether an algebraic expression can always simplify to zero.

    Input

    The first line contains a single integer T, indicating the number of test cases.
    Each test case starts with an integer N, the number of tokens that describes a formula. The next N tokens describe a formula in reverse polish notation.
    The notation works as follows. There is a stack that begins empty, and only the following commands manipulate the contents of the stack:
    1. “x” pushes the variable x to the stack.
    2. “sin”, “cos”, and “tan” replace the top element of the stack with its sin, cos, and tan, respectively.
    3. “+”, “-”, and “*” replace the top two elements of the stack (a on top, followed by b) with their sum(b + a), difference (b − a), and product (b ∗ a), respectively.
    You may assume that the input is valid, and results in a single item on the stack, which is the desired expression. The length of a line will be at most 300 characters. Note function arguments can contain functions.
     

    Output

    For each test case, output the case number first, then “Yes” if the expression is always zero, otherwise output “No”.
     

    Sample Input

    2
    3 x sin sin
    15 x sin x sin * x cos x cos * + x * x -
     

    Sample Output

    Case 1: No
    Case 2: Yes
    题意:判断表达式是否恒为0
    思路:因为三角函数都是周期性变化的,所以我们可以枚举x来计算,但是精度自己内心也不确定,就按0.0001的精度累加,也A了
     
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <stack>
    #include <math.h>
    using namespace std;
    
    #define pi acos(-1.0)
    
    stack<double> S;
    char str[305][10];
    
    int main()
    {
        int t,n,i,j,cas = 1;
        int flag;
        double x,y,z;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            for(i = 0; i<n; i++)
                scanf("%s",str[i]);
            flag = 1;
           for(x = -2.0; x<=2.0; x+=0.0001)//枚举x
            {
                for(j = 0; j<n; j++)
                {
                    if(!strcmp(str[j],"x"))
                        S.push(x);
                    else if(!strcmp(str[j],"sin"))
                    {
                        y = S.top();
                        S.pop();
                        y = sin(y);
                        S.push(y);
                    }
                    else if(!strcmp(str[j],"cos"))
                    {
                        y = S.top();
                        S.pop();
                        y = cos(y);
                        S.push(y);
                    }
                    else if(!strcmp(str[j],"tan"))
                    {
                        y = S.top();
                        S.pop();
                        y = tan(y);
                        S.push(y);
                    }
                    else if(!strcmp(str[j],"+"))
                    {
                        y = S.top();
                        S.pop();
                        z = S.top();
                        S.pop();
                        y = y+z;
                        S.push(y);
                    }
                    else if(!strcmp(str[j],"-"))
                    {
                        y = S.top();
                        S.pop();
                        z = S.top();
                        S.pop();
                        y = z-y;
                        S.push(y);
                    }
                    else if(!strcmp(str[j],"*"))
                    {
                        y = S.top();
                        S.pop();
                        z = S.top();
                        S.pop();
                        y = y*z;
                        S.push(y);
                    }
                }
                y = S.top();
                S.pop();
                if(fabs(y)<1e-8 && S.empty())
                    continue;
                else
                {
                    flag = 0;
                    break;
                }
            }
            printf("Case %d: ",cas++);
            if(flag)
                printf("Yes
    ");
            else
                printf("No
    ");
        }
    
        return 0;
    }
    

  • 相关阅读:
    VS2010-MFC(对话框:模态对话框及其弹出过程)
    VS2010-MFC(对话框:设置对话框控件的Tab顺序)
    VS2010-MFC(对话框:为控件添加消息处理函数)
    VS2010-MFC(对话框:创建对话框类和添加控件变量)
    manacher算法模板
    [洛谷P1017] 进制转换
    [洛谷P1126] 机器人搬重物
    [洛谷P1032] 字串变换
    [POI2005]SAM-Toy Cars
    [洛谷P1528] 切蛋糕
  • 原文地址:https://www.cnblogs.com/james1207/p/3353176.html
Copyright © 2011-2022 走看看