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\n");
else
printf("No\n");
} return 0;
}

BNU29368:Check the Identity(栈)的更多相关文章

  1. bnuoj 29368 Check the Identity(栈)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=29368 [题解]:模拟,然后对x,进行枚举,看是否所有都满足条件 [code]: #include ...

  2. 【ASP.NET Identity系列教程(二)】运用ASP.NET Identity

    注:本文是[ASP.NET Identity系列教程]的第二篇.本系列教程详细.完整.深入地介绍了微软的ASP.NET Identity技术,描述了如何运用ASP.NET Identity实现应用程序 ...

  3. ASP.NET Identity 二 (转载)

    来源:http://www.cnblogs.com/r01cn/p/5180892.html#undefined 推荐看原文,这里转载是怕好文章消失了. 注:本文是[ASP.NET Identity系 ...

  4. ASP.NET Identity系列教程-3【运用ASP.NET Identity】

    https://www.cnblogs.com/r01cn/p/5180892.html 14 运用ASP.NET Identity In this chapter, I show you how t ...

  5. 关于refs/for/ 和refs/heads/

    1.     这个不是git的规则,而是gerrit的规则, 2.     Branches, remote-tracking branches, and tags等等都是对commite的引用(re ...

  6. CLR via C# 3rd - 05 - Primitive, Reference, and Value Types

    1. Primitive Types        Any data types the compiler directly supports are called primitive types. ...

  7. [转]Installing SharePoint 2013 on Windows Server 2012 R2

    转自:http://www.avivroth.com/2013/07/09/installing-sharepoint-2013-on-windows-server-2012-r2-preview/ ...

  8. 《Effective C#》:区别和认识四个判等函数

    .Net有四个判等函数?不少人看到这个标题,会对此感到怀疑.事实上确是如此,.Net提供了ReferenceEquals.静态Equals,具体类型的Equals以及==操作符这四个判等函数.但是这四 ...

  9. 【转】区别和认识.Net四个判等函数

    原文地址:不详 .Net有四个判等函数?不少人看到这个标题,会对此感到怀疑.事实上确是如此,.Net提供了ReferenceEquals.静态Equals,具体类型的Equals以及==操作符这四个判 ...

随机推荐

  1. items 与iteritems

    dict的items函数返回的是键值对的元组的列表,而iteritems使用的是键值对的generator. items当使用时会调用整个列表 iteritems当使用时只会调用值. >> ...

  2. 解决自定义BackItem与Pop Gesture冲突的问题

    在做项目的时候遇到的这个问题, 一开始项目要求自定义导航栏返回按钮,结果发生了没法手势返回的问题,以为是需要添加拖拽手势呢,结果折腾了一下午没有实现想要的效果.接着一直百度问题,才发现跑偏了,犯了一个 ...

  3. springmvc学习(三)

    第一点---------使用 @RequestMapping 映射请求• Ant 风格资源地址支持 3 种匹配符:?:匹配文件名中的一个字符 *:匹配文件名中的任意字符 **:** 匹配多层路径 @R ...

  4. 九度OJ 1408 吃豆机器人 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1408 题目描述: 淘宝公司内部有许多新鲜的小玩具,例如淘宝智能机器人.小时候,大家都玩过那个吃豆子的游戏吧,这机器 ...

  5. html元素li移动动态效果

    在日常工作当中遇到了一个问题,平铺型列表修改单个内容设置排序时列表排序应与之对应.一下是一个小小的例子:简单的解决了此类问题,以浮动的形式改变当前的数据的显示顺序.有不足之处欢迎指点,后期还会做一个更 ...

  6. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  7. [可拖动DIV]刚开通博客顺便就写了点东西!

    说说我自己的思路 首先需要一个初始div div { border: 1px #333 solid; width: 200px; height: 50px; } <div id="od ...

  8. gdb提示Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6_5.2.x86_64

    用gdb debugc代码的时候弹出这个错误 Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6_5.2. ...

  9. php中调用mysql的存储过程和存储函数

    //$sql = 'call del()';  调用存储过程 del(参数列表)//mysql_query($sql); $sql = "insert into t values (1, f ...

  10. iOS局部刷新

    iOS: TableView如何刷新指定的cell 或section //一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithInd ...