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

转载于:https://www.cnblogs.com/qscqesze/p/5136037.html

Codeforce Gym 100015I Identity Checker 暴力相关推荐

  1. Codeforces gym 100685 A. Ariel 暴力

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

  2. code Gym 100500D T-shirts(暴力)

    因为只能买一次,暴力枚举一下买的衣服的大小. #include<cstdio> #include<map> #include<algorithm>using nam ...

  3. codeforce Gym 100500F Door Lock (二分)

    根据题意略推一下,其实就是问你满足(a*(a+1))/2 < m <= ((a+1)*a(a+2))/2的a和m-(a*(a+1))/2 -1是多少. 二分求解就行了 #include&l ...

  4. codeforce gym 100548H The Problem to Make You Happy

    题意: Alice和Bob在一个有向图上玩游戏,每个人各自操作一个棋子,如果两个棋子走到一个点上,判定Bob输:如果轮到任何一方走时,无法移动棋子,判定该方输 现在Bob先走,要求判断胜负 题解 模型 ...

  5. POJ-1035 Spell checker 暴力

    直接暴力. 代码如下: #include <cstring> #include <cstdio> #include <cstdlib> #include <m ...

  6. codeforce Gym 101102A Coins (01背包变形)

    01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...

  7. codeforce Gym 100418K Cards (概率,数学)

    题意:麦田的故事,n张牌,取x张牌,记住前x张牌最大的值m,继续往后取,遇到第一张比m大的牌就停下来.求一个x使得最后的牌在整副牌里是最大的期望最大. 假设最大的牌是A,A在各种位置出现的概率就是相等 ...

  8. codeforce Gym 100685E Epic Fail of a Genie(MaximumProduction 贪心)

    题意:给出一堆元素,求一个子集,使子集的乘积最大,如有多个,应该使子集元素个数尽量小. 题解:贪心,如果有大于1的正数,那么是一定要选的,注意负数也可能凑出大于1的正数,那么将绝对值大于1的负数两两配 ...

  9. Stanford Local Programming Contest 2011

    这几天把SLPC2011的题目做一下 这里是题目连接:SLPC2011 A.Another Rock-Paper-Scissors Problem 题目大意:给出Sonny在每一局出石头剪刀布的规则( ...

  10. Codeforces Gym 100513G G. FacePalm Accounting 暴力

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

最新文章

  1. Java好文统计( 引用 )
  2. Leetcode题库 5.最长回文子串(C++实现)
  3. 杨氏矩阵定义及其查找的实现C++
  4. Error Domain=NSURLErrorDomain Code=-999 The opera
  5. SAP在Kubernetes上打造的Kyma到底是个什么东东
  6. 用Python和Pygame写游戏-从入门到精通(1)
  7. sheets.add示例_Java ArrayDeque add()方法与示例
  8. 罗永浩直播间再回应直播售假:全方位整改 成立质控实验室
  9. 了不起的Node.js: 将JavaScript进行到底(Web开发首选,实时,跨多服务器,高并发)
  10. CentOS 6.3 配置 yum
  11. 常用图像处理库都有哪些?
  12. python中ctype的应用,协议解析,C语言与python的完美映射,结构体与字符串的相互转换
  13. 多线程实现同步摄像头录像(Multi-threading to achieve synchronized camera recording)
  14. linux 查询ip归属地的工具,Linux下查询IP归属地的实现
  15. Flink On K8S终极实现方案
  16. Unity 动画模拟果冻抖动效果
  17. Java之------常用的设计模式
  18. Hive Sql 分析实例: 淘宝消费者行为分析
  19. android刷广告,android – 广告不可见.没有刷新广告.屏幕关闭后
  20. C语言扫雷(附标记展开功能)

热门文章

  1. python绘图之Times New Roman字体以及Helvetica字体
  2. 背篼酥课堂-GPS定位(一) nodemcu 解析gps
  3. angular使用echarts词云图wordCloud
  4. poj 2632 Crashing Robots
  5. Up in the Air-19
  6. 乐队的夏天大结局!用Python分析投票数据,选出真正的乐队TOP 5
  7. 对立志成为DBA的人的几点建议
  8. 读完这篇系列文章,前端offer手到擒来!!!
  9. 在苹果手机上实现虹膜识别(通过改装实现)
  10. Linux开发板网络直连电脑的设置方法