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以及==操作符这四个判 ...
随机推荐
- C#基于AE组件二次开发常见问题
由于本人从事的是在.net平台下进行GIS的二次开发,所以第一篇博文就说一下:我最近在项目中出现的常见的问题,如果能够给大家增加一点点便利,也是我的荣幸,如果大家对于这次博文有什么意见和建议,欢迎大家 ...
- Harry Potter
Names appearing in "Harry Potter" 1.Harry Potter ①Harry is from Henry. ②Harry is related t ...
- MSBuild could not create or connect to a task host with runtime "CLR2" and architecture "x86".
vs2010 and vs2012 are installed on target machine. Build c# project using vs2010, following error oc ...
- Objective-C 之同步请求、异步请求、GET请求、POST请求
本篇文章由:http://www.sollyu.com/objective-c-synchronization-requests-requests-for-asynchronous-requests- ...
- 【转载】C#后台声明式验证,远离if验证
ViewModel public class ViewModel { [Required(ErrorMessage="标题不能为空")] public string Title { ...
- mysql用户管理,权限管理
mysql权限 相关操作: 授予的权限分为四组: 列权限:和表中的一个具体列相关,例如:使用update 语句更新test表中name 列的值 表权限:和一个具体的表的所有数据相关,例如:使用 sel ...
- 《jQuery UI开发指南》勘误收集
此书由罗晴明 (http://weibo.com/sunnylqm)和我合译完成,此篇博客作为勘误收集而用,若译文有误或者有任何疑问,欢迎留下评论,或者给我发邮件(地址:gzooler@gmail.c ...
- 【转】循环递归遍历XML文档或按某要求遍历XML文档
<?xml version="1.0" encoding="UTF-8"?> <catalog> <cd> <titl ...
- js中的callback(阻塞同步或异步时使用)
1.回调就是一个函数的调用过程,函数a有一个参数,这个参数是个函数b,当函数a执行完以后执行函数b, 那么这个过程就叫回调 eg. function a(callback){ alert('paren ...
- Redhat 6.5 x64 下载地址
http://ftp.okhysing.is/ftp/redhat/6.5/isos/x86_64/