Identity Checker

题目连接:

http://codeforces.com/gym/100015/attachments

Description

You likely have seen that x(sin x +cos2 x) ! x = 0, and you may have seen that sin(2x) ! 2 sin x cos x =0.
But did you know that tan (2x)(x ! x tan2 x) ! 2x tan x = 0? Would you believe that sin (2x) ! 2cos x =0?
That last one is false, but don’t just take our word for it; you should write a program that determines
whether an algebraic expression simplifies to zero (whenever it is defined).

Input

The input consists of multiple test cases, each on one line. 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 the following commands manipulate
the contents of the stack:

• “x” pushes the variable x to the stack.
• “sin”, “cos”, and “tan” replace the top element of the stack with its sin, cos, and tan, respectively.
• “+”, “-”, and “*” replace the top two elements of the stack (a on top, followed by b) with their sum
(b + a), di!erence (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. Function arguments can contain functions,
so xsinsin is valid, but the recursion will not go any deeper than this. The input terminates with a line
with N = 0. For example:

Output

For each test case, print out a single line that contains “Identity” if the expression is always zero, and
“Not an identity” otherwise (quotes added for clarity). For example, the correct output for the sample
input above would be:

Sample Input

15 x sin x sin * x cos x cos * + x * x -

16 x sin x cos * x sin x cos * + x x + sin -

24 x x + tan x x tan x tan * x * - * x tan x * - x tan x * -

10 x x + sin x cos - x cos -

0

Sample Output

Identity

Identity

Identity

Not an identity

Hint

题意

给你一个后缀表达式子,只含有sin,cos,tan,+,-,*,x

然后问你这个式子答案是否恒等于0

题解:

直接扔随便几个数去跑,如果全部跑出来0

那就是恒等于0了咯~

注意,这道题精度好像很蛋疼。。。

代码

#include<bits/stdc++.h>
using namespace std;


string s[1000];
double check(int n,double x)
{
    stack<double> t;
    for(int i=0;i<n;i++)
    {
        if(s[i]=="x")
            t.push(x);
        if(s[i]=="sin")
        {
            double tmp = t.top();
            t.pop();
            t.push(sin(tmp));
        }
        if(s[i]=="cos")
        {
            double tmp = t.top();
            t.pop();
            t.push(cos(tmp));
        }
        if(s[i]=="tan")
        {
            double tmp = t.top();
            t.pop();
            t.push(tan(tmp));
        }
        if(s[i]=="+")
        {
            double tmp1 = t.top();
            t.pop();
            double tmp2 = t.top();
            t.pop();
            t.push(tmp2+tmp1);
        }
        if(s[i]=="-")
        {
            double tmp1 = t.top();
            t.pop();
            double tmp2 = t.top();
            t.pop();
            t.push(tmp2-tmp1);
        }
        if(s[i]=="*")
        {
            double tmp1 = t.top();
            t.pop();
            double tmp2 = t.top();
            t.pop();
            t.push(tmp2*tmp1);
        }
    }
    //cout<<t.top()<<endl;
    return t.top();
}
vector<double> ans;
int main()
{
    //freopen("1.in","r",stdin);
    int n;
    while(cin>>n)
    {
        if(n==0)
            break;
        for(int i=0;i<n;i++)
            cin>>s[i];
        ans.clear();
        ans.push_back(check(n,213));
        ans.push_back(check(n,1.0));
        ans.push_back(check(n,123));
        ans.push_back(check(n,90));
        ans.push_back(check(n,9871));
        ans.push_back(check(n,3.1234));
        ans.push_back(check(n,-1231.5));
        ans.push_back(check(n,0));
        int flag = 0;
        for(int i=0;i<ans.size();i++)
        {
            if(fabs(ans[i])>1e-9)
            {
                cout<<"Not an identity"<<endl;
                flag = 1;
                break;
            }
        }
        if(flag==0)
            cout<<"Identity"<<endl;
    }
}