BNU29368:Check the Identity(栈)
Just determine whether an algebraic expression can always simplify to zero.
Input
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
Sample Input
3 x sin sin
15 x sin x sin * x cos x cos * + x * x -
Sample Output
Case 2: Yes
#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(栈)的更多相关文章
- bnuoj 29368 Check the Identity(栈)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=29368 [题解]:模拟,然后对x,进行枚举,看是否所有都满足条件 [code]: #include ...
- 【ASP.NET Identity系列教程(二)】运用ASP.NET Identity
注:本文是[ASP.NET Identity系列教程]的第二篇.本系列教程详细.完整.深入地介绍了微软的ASP.NET Identity技术,描述了如何运用ASP.NET Identity实现应用程序 ...
- ASP.NET Identity 二 (转载)
来源:http://www.cnblogs.com/r01cn/p/5180892.html#undefined 推荐看原文,这里转载是怕好文章消失了. 注:本文是[ASP.NET Identity系 ...
- 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 ...
- 关于refs/for/ 和refs/heads/
1. 这个不是git的规则,而是gerrit的规则, 2. Branches, remote-tracking branches, and tags等等都是对commite的引用(re ...
- CLR via C# 3rd - 05 - Primitive, Reference, and Value Types
1. Primitive Types Any data types the compiler directly supports are called primitive types. ...
- [转]Installing SharePoint 2013 on Windows Server 2012 R2
转自:http://www.avivroth.com/2013/07/09/installing-sharepoint-2013-on-windows-server-2012-r2-preview/ ...
- 《Effective C#》:区别和认识四个判等函数
.Net有四个判等函数?不少人看到这个标题,会对此感到怀疑.事实上确是如此,.Net提供了ReferenceEquals.静态Equals,具体类型的Equals以及==操作符这四个判等函数.但是这四 ...
- 【转】区别和认识.Net四个判等函数
原文地址:不详 .Net有四个判等函数?不少人看到这个标题,会对此感到怀疑.事实上确是如此,.Net提供了ReferenceEquals.静态Equals,具体类型的Equals以及==操作符这四个判 ...
随机推荐
- 牛客_剑指offer_重建二叉树,再后续遍历_递归思想_分两端
总结: 重建二叉树:其实就是根据前序和中序重建得到二叉树,得到后续,只要输出那边设置输出顺序即可 [编程题]重建二叉树 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的 ...
- 利用wireshark抓取Telnet的用户名和密码
使用wireshark抓取Telnet 目标ip地址(telnet 192.168.88.1 ) 1,首先打开wireshark,然后选择网卡,点击开始. 2,为了在filter中输入telne ...
- WPF的TextBox的焦点获取与失去焦点的死循环解决方案
在WPF中实现一个弹出层自动获取焦点,弹出层实现是通过其UserControl的依赖属性Visibility的绑定实现的,让UserControl上的TextBox获取焦点,初始实现代码如下: pub ...
- C#与C++相比较之STL篇(续一)
本篇接<C#与C++相比较之STL篇>,主要探索C++STL的两个组件:算法和仿函数,以及C#的linq和拉姆达表达式.委托. STL的算法与仿函数 算法是个庞大的主题,STL包含了超过1 ...
- linux 下使用crontab 定时打包日志并删除已被打包的日志
crontab是和用户相关的,每个用户有自己对应的crontab . cron是Linux下的定时执行工具,以下是重启/关闭等等的命令 #/sbin/service crond start //启动服 ...
- ssh: connect to host xxx.xxx.xxx.xxx port 22: Connection refused
工具/原料:Ubuntu 在采用scp在不同机器之间进行文件拷贝时出现标题所示的错误,原因可能是: 1.sshd 未启动 2.sshd 未安装 3.防火墙 4需重新启动ssh 服务 查看sshd进程方 ...
- 一个简单的Inno Setup例子
; 脚本由 Inno Setup 脚本向导 生成! ; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档! [Setup] ; 注: AppId的值为单独标识该应用程序. ; 不要为其 ...
- JSON Date Format/JSON 日期格式方法分享
我是很懒的,不想多说,所以直接上代码.亲们懂的. <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://w ...
- Application.StartupPath同System.Environment.CurrentDirectory区别
System.Windows.Forms.Application.StartupPath:获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称. System.Environment.Curr ...
- 关于window.location不跳转的问题
今天在码代码的时候遇到个问题:html里采用onclick事件来实现window.location = url的跳转,在内嵌元素上又加上了href="javascrit:;"的属性 ...