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以及==操作符这四个判 ...
随机推荐
- 自动生存Makefile教程 autoscan aclocal autoconf autoheader automake configure
LZ没学过makefile的写法,只知道使用tab.于是乎发现了autotools系列工具 基本流程是:autoscan.aclocal.autoconf.autoheader.automake.co ...
- ###学习《C++ Primer》- 1
点击查看Evernote原文. #@author: gr #@date: 2014-09-30 #@email: forgerui@gmail.com 记录读书过程中一些知识点.可能不系统,:-). ...
- [记录 ]升级IOS 9 和 XCode 7 引起的问题
问题一: 升级xcode 7最低的系统配置要求 升级了ios9 后使用 xcode 6.1 已经不能用了,必须升级 xcode 7才行,原先的系统是OSX 10.10.1 版本.而xcode 7.0 ...
- 几个动画demo
一.点击扩散效果 这个效果没什么难度,主要是加深对核心动画的理解和使用,但是还是有几个想说明的地方.先看一下效果,具体内容代码里有注释. // // CircleButton.m // UITest ...
- OC1_银行账户类
// // BankAccount.h // OC1_银行账户类 // // Created by zhangxueming on 15/6/10. // Copyright (c) 2015年 zh ...
- Dorado浏览器调试
通常在项目中我们对js脚本进行调试有以下2种方式: alert调试法 首先是最原始也是最简单的使用alert,在页面中需要输出需要的变量的地方加上alert函数,将变量弹出显示:alert方式虽然简单 ...
- Web前端新人笔记之CSS值和单位
数字 颜色——命名颜色 在Css2.1中规范定义了17个颜色名.包括html4.0中定义的16个颜色及外加一个橙色: <h1 style="color=aqua">aq ...
- 图片裁切插件jCrop的使用心得(三)
在这一篇里,我来具体讲讲代码该如何写. 下面是jCrop的初始化代码 //图片裁剪插件Jcrop初始化 function initJcrop() { // 图片加载完成 document.getEle ...
- Vijos p1002 过河 离散化距离+区间DP
链接:https://vijos.org/p/1002 题意:一条长度为L(L <= 1e9)的桥上有N(1<= N <= 100)颗石头.桥的起点为0终点为L.一只青蛙从0开始跳, ...
- codeforces 8VC Venture Cup 2016 - Elimination Round C. Lieges of Legendre
C. Lieges of Legendre 题意:给n,m表示有n个为2的倍数,m个为3的倍数:问这n+m个数不重复时的最大值 最小为多少? 数据:(0 ≤ n, m ≤ 1 000 000, n + ...