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;
}
}

Codeforce Gym 100015I Identity Checker 暴力的更多相关文章

  1. Codeforces Gym 100015H Hidden Code 暴力

    Hidden Code 题目连接: http://codeforces.com/gym/100015/attachments Description It's time to put your hac ...

  2. Codeforces gym 100685 A. Ariel 暴力

    A. ArielTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Desc ...

  3. Codeforces Gym 100637G G. #TheDress 暴力

    G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G ...

  4. codeforce gym/100495/problem/K—Wolf and sheep 两圆求相交面积 与 gym/100495/problem/E—Simple sequence思路简述

    之前几乎没写过什么这种几何的计算题.在众多大佬的博客下终于记起来了当时的公式.嘚赶快补计算几何和概率论的坑了... 这题的要求,在对两圆相交的板子略做修改后,很容易实现.这里直接给出代码.重点的部分有 ...

  5. Gym 101055A 计算几何,暴力

    http://codeforces.com/gym/101055/problem/A 题目:给定一些三维空间的点,要你找一个平面,能覆盖尽量多的点,只要求输出点数即可.n<=50 因为数据量小, ...

  6. Codeforces Gym 100203G Good elements 暴力乱搞

    原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑暴力的复杂度是O(n^3),所以 ...

  7. Spell checker(暴力)

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20188   Accepted: 7404 De ...

  8. Gym - 101848B Almost AP 暴力

    题目链接:http://codeforces.com/gym/101848/problem/B 给出一串数字要你最多改动三个数字使这一串数字成为等差数列.因为最多改动三个数字所以可以先求出相邻两项的差 ...

  9. codeforce Gym 100500A Poetry Challenge(博弈,暴搜)

    题解:状态压缩之后,暴力dfs,如果有一个选择,能让对手必败,那么就是必胜态,能转移到的状态都是对手的必胜态,或者无法转移,就是必败态. 总算是过了,TLE是因为状态没判重. #include< ...

随机推荐

  1. NSIS学习笔记(转)

    转自:http://blog.csdn.net/lee353086/article/details/45919901 NSIS学习笔记Date:2015-05-20Author:kagulaEnv:V ...

  2. nagios监控远程主机端口

    1 被监控主机上的操作 修改nrpe插件内容: 在其中增加的内容如下: 表示的含义为监控主机的端口631和661,这个主要是监控命令 重启xinetd服务: 2 监控主机上的操作 查看监控命令配置文件 ...

  3. Java中实现异常处理的基础知识

    Java中实现异常处理的基础知识 异常 (Exception):发生于程序执行期间,表明出现了一个非法的运行状况.许多JDK中的方法在检测到非法情况时,都会抛出一个异常对象. 例如:数组越界和被0除. ...

  4. Window.onLoad 和 DOMContentLoaded事件的先后顺序

    相信写js的,都知道window.onload吧,但是并不是每个人都知道DOMContentLoaded,其实即使你不知道,很有可能你也经常使用了这个东西. 一般情况下,DOMContentLoade ...

  5. 浅谈w3c标准

    #浅谈w3c标准 ##w3c标准是什么 w3c标准包括多个方面,官方是从应用角度分的,相关的文档可以戳[这里](http://www.w3.org/standards/).如果从WEB技术角度,可以分 ...

  6. 项目常用jquery/easyui函数小结

    #项目常用jquery/easyui函数小结 ##背景 项目中经常需要使用到一些功能,封装.重构.整理后形成代码沉淀,在此进行分享 ##代码 ```javascript /** * @author g ...

  7. Weibo Crawler in Action

    1.要写一个微博爬虫,得分开几个模块来做: (1)模拟登录 (2)模拟浏览 (3)针对短时间内大量访问而引起怀疑的禁止登陆解决方案 (4)其他 (1)模拟登陆模块 前提:要模拟登录,得首先知道在登录微 ...

  8. poj2528(线段树+离散化)Mayor's posters

    2016-08-15 题意:一面墙,往上面贴海报,后面贴的可以覆盖前面贴的.问最后能看见几种海报. 思路:可以理解成往墙上涂颜色,最后能看见几种颜色(下面就是以涂色来讲的).这面墙长度为1~1000 ...

  9. 轻松学习Linux系统安装篇之fdisk命令行工具的使用

    fdisk 的介绍:          fdisk 命令是磁盘分区表操作工具:和以前Dos和windows下的分区工具功能一样:fdsik 能划分磁盘成为若干个区,同时也能为每个分区指定分区的文件系统 ...

  10. this compilation unit is not on the build of a java project

    this compilation unit is not on the build of a java project java里输入代码就出这个提示,无法写代码了. 找到觉得路径打开文件编辑就好了, ...