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. delphi TeeChart保存3种图片文件

    var vForm: Tfrm_ChemaShowMainChild;begin vForm := GetActiveForm; vForm.cht_Edit.SaveToMetafile('C:\1 ...

  2. 链接服务器"(null)"的 OLE DB 访问接口 "Microsoft.Jet.OLEDB.4.0" 返回了消息 "未指定的错误"。[手稿]

    消息 7302,级别 16,状态 1,第 1 行 无法创建链接服务器 "(null)" 的 OLE DB 访问接口 "Microsoft.JET.OLEDB.4.0&qu ...

  3. 11个实用经典的SQL小贴士

    学习工作之余,在没有要解决问题的压力之下,还是建议系统的看看书,对于一些认为没啥用的知识点,也建议去仔细的看看,练练手,说不定什么时候就用到了,到时也好有针对性的去查,不至于盲目的按照自己的思路,重复 ...

  4. 重新开始吧(ADB+AndroidManifest.xml)

    我现在默认已经搭建好了开发环境.如果没有,可以参见去Google一下,或者我上两篇文章中也有提到. 先补充一点: SDK不用FQ.也能更新 修改hosts文件 下载sdk版本: 在hosts文件中追加 ...

  5. 一个Web页面的生命周期 ,面试常常被问到

    常规页生命周期阶段 一般来说,页要经历下表概述的各个阶段.除了页生命周期阶段以外,在请求前后还存在应用程序阶段,但是这些阶段并不特定于页.有关更多信息,请参见 ASP.NET 应用程序生命周期概述. ...

  6. Winsock IO模型之select模型

    之所以称其为select模型是因为它主要是使用select函数来管理I/O的.这个模型的设计源于UNIX系统,目的是允许那些想要避免在套接字调用上阻塞的应用程序有能力管理多个套接字. int sele ...

  7. 从四大音乐APP首页设计对比分析产品方向

    原帖:http://www.ui.cn/detail/63201.html 本文章中作者例举四个音乐APP应用:虾米.网易.百度.QQ首页 1. 推荐内容:作者将四个首页界面划分出官方推荐与个性化推荐 ...

  8. 使用Java进行MD5加密

    使用Java自带的MessageDigest类可以轻松实现MD5加密,只不过加密后得到的是byte数组,我们需要将其转换为16进制的字符. 代码如下: package com.stepsoft.tes ...

  9. [翻译]创建ASP.NET WebApi RESTful 服务(8)

    本章讨论创建安全的WebApi服务,到目前为止,我们实现的API都是基于未加密的HTTP协议,大家都知道在Web中传递身份信息必须通过HTTPS,接下来我们来实现这一过程. 使用HTTPS 其实可以通 ...

  10. android错误系列之导出数据库出错Failed to pull selection

    使用效率检视工具traceView,在导出检测文件时,出现了“failed to pull a selection”问题,网上搜索了几篇文章,有的说,是因为导出超时,我将windows-->pr ...