题意:给出一个后缀表达式f(x),最多出现一次x,解方程f(x) = 0。

读取的时候用一个栈保存之前的结点,可以得到一颗二叉树,标记出现'X'的路径,先把没有出现'X'的子树算完,由于读取建树的时候是由底向上的,

这步可以在读取的时候顺带完成。

注意'X'或'1/x'在某个结点和'0'相乘,那么'X'等效与没有出现过,把之后的结点标记为常数。

然后dfs模拟运算和移项。

还有一些输入输出的小细节和几组测试数据,具体看代码。

WA了很多发,去找数据手动对拍好久终于发现(1/(1/x))=0这种情况是被当作无解。。。而我当作x = 0来处理了,QAQ。为何解个一元一次如此艰辛。。。

第一次写5000B+,模拟真的很难写有没有。手写一个分数的模板

#include<bits/stdc++.h>
using namespace std; const int maxn = ;
typedef long long ll; ll gcd(ll a,ll b) { return b?gcd(b,a%b):a; } struct Fra
{
ll p,q;
Fra(ll x = ,ll y = ):p(x),q(y){ normal(p,q); }
void normal(ll &p,ll &q) { ll g = gcd(p,q); p/=g; q/=g; }
Fra operator = (int x) { p = x; q = ; return *this; }
Fra operator = (ll x) { p = x; q = ; return *this; }
Fra operator - () { return {-p,q}; }
Fra operator + (Fra &r) {
ll m,n;
m = p*r.q+r.p*q;
n = q*r.q;
normal(m,n);
return {m,n};
}
Fra operator += (Fra& r) { return *this = *this+r; }
Fra operator - (Fra &r) { return (-r) + *this; }
Fra operator -= (Fra &r) { return *this = *this-r; }
Fra operator * (Fra &r) {
ll m,n;
m = p*r.p;
n = q*r.q;
normal(m,n);
return {m,n};
}
Fra operator *= (Fra &r) { return (*this) = (*this)*r; }
Fra operator /(Fra &r) { return Fra(r.q,r.p) * (*this); }
Fra operator /=(Fra &r) { return (*this) = (*this)/r; }
bool operator == (const Fra& r) const { return p*r.q == r.p*q; }
bool operator < (Fra& r) { return p*r.q < r.p*q; }
void print() { normal(p,q); if(q<)q = -q,p = -p; printf("%lld/%lld\n",p,q); }
}; struct Node
{
Node* l,*r;
Fra f; char op;
bool fx;
Node(){};
Node(Fra &v,Node*a = NULL, Node*b = NULL):f(v),l(a),r(b){}; }nd[maxn]; bool isOp[];
char rev[]; Fra cal(Fra &x,Fra &y,char op)
{
//assert(isOp[op] == true)
switch(op){
case '+':return x+y;
case '-': return x-y;
case '*': return x*y;
case '/': return x/y;
}
return {,};
} Fra ans; void calRev(Fra &x,char op)
{
switch(op){
case'+':ans-=x; return;
case'*':ans/=x; return ;
case'-':ans = x-ans; return;
case'/':ans = x/ans; return;
}
} //之前要预处理
bool dfs(Node* u)
{
//*u;
if(u->l == NULL) return true;
//assert(u.r)
if(u->l->fx){
ans = cal(ans,u->r->f,rev[u->op]); //乘以0的情况已经预处理了
if(!dfs(u->l)) return false;
}else if(u->r->fx) {
calRev(u->l->f,u->op);//移项,ans本身可能会是0
if(ans.q == ) { return false; }
if(!dfs(u->r)) return false;
}
return true;
} Node* read(char ch)
{
int cnt = ;
stack<Node*> stk;
do{
while(ch == ' ')ch = getchar();
Node &cur = nd[cnt];
if(isOp[ch]){
cur.op = ch;
cur.r = stk.top(); stk.pop();
cur.l = stk.top(); stk.pop();
cur.fx = cur.l->fx || cur.r->fx;
if(cur.fx){ //系数为0的处理
if((cur.op == '*' && (cur.r->fx ? cur.l->f == : cur.r->f == ))
|| (cur.op == '/' && cur.r->fx && cur.l->f == ) ) {
cur.fx = false;
cur.f = ; cur.l = cur.r = NULL;
}
}else { //预处理,边读边算
cur.f = cal(cur.l->f,cur.r->f,cur.op);
cur.l = cur.r = NULL;
}
}else {
if(ch == 'X'){
cur.fx = true;
}else {
cur.fx = false;
int data = ch-'';
while(ch = getchar(), ch>=''&&ch<='') data = data*+ch-'';
cur.f = data;
}
cur.l = cur.r = NULL;
}
stk.push(nd+cnt);
ch = getchar(); cnt++;
}while(ch != '\n'&&~ch);
return stk.top();
} int main()
{
//freopen("in.txt","r",stdin);
isOp['+'] = isOp['-'] = isOp['*'] = isOp['/'] = true;
rev['+'] = '-'; rev['-'] = '+'; rev['*'] = '/'; rev['/'] = '*';
char head;
while(~(head = getchar())){
Node* root = read(head);
if(!root->fx) {
if(root->f == ) puts("MULTIPLE");
else puts("NONE");
continue;
}
ans = ;
if(dfs(root)){ printf("X = "); ans.print(); }
else {puts("NONE"); continue; }
}
return ;
} /*
1 1 X / /
1 1 X 2 - / / 1 X /
0 X * 1 +
0 X / 1 + 9 9 9 9 9 9 9 9 9 9 9 9 9 X 1 * * * * * * * * * * * * * +
7 6 - 8 / 8 2 * / 0 3 - + 5 5 1 / + 1 X 2 + + 8 8 + + * +
7 8 X + * 1 8 5 7 3 + * * 7 6 6 / 3 + + 6 5 7 / - * / / +
*/

UVA 1661 Equation (后缀表达式,表达式树,模拟,实现)的更多相关文章

  1. 表达式目录树(Expression)

    一:什么是表达式树 Expression我们称为是表达式树,是一种数据结构体,用于存储需要计算,运算的一种结构,这种结构可以只是存储,而不进行运算.通常表达式目录树是配合Lambda一起来使用的,la ...

  2. MVC图片上传详解 IIS (安装SSL证书后) 实现 HTTP 自动跳转到 HTTPS C#中Enum用法小结 表达式目录树 “村长”教你测试用例 引用provinces.js的三级联动

    MVC图片上传详解   MVC图片上传--控制器方法 新建一个控制器命名为File,定义一个Img方法 [HttpPost]public ActionResult Img(HttpPostedFile ...

  3. 05.表达式目录树Expression

    参考文章 https://www.cnblogs.com/xyh9039/p/12748983.html 1. 基本了解 1.1 Lambda表达式 演变过程 using System; namesp ...

  4. LinQ实战学习笔记(三) 序列,查询操作符,查询表达式,表达式树

    序列 延迟查询执行 查询操作符 查询表达式 表达式树 (一) 序列 先上一段代码, 这段代码使用扩展方法实现下面的要求: 取进程列表,进行过滤(取大于10M的进程) 列表进行排序(按内存占用) 只保留 ...

  5. C#表达式目录树(Expression)

    1.什么是表达式目录树 :简单的说是一种语法树,或者说是一种数据结构(Expression) 2.用Lambda声明表达式目录树: Expression<Func<; //表达试目录树的方 ...

  6. C#_表达式目录树的应用

    使用表达式目录树实现两个不同类型的属性赋值: public class People { public int Age { get; set; } public string Name { get; ...

  7. 第十九节: 结合【表达式目录树】来封装EF的BaseDal层的方法

    一. 简介 该章节,可以说是一个简单轻松的章节,只要你对Expression表达式树.EF的基本使用.泛型有所了解,那么本章节实质上就是一个非常简单的封装章节,便于我们快捷开发. PS:在该章节对于E ...

  8. 第十五节:Expression表达式目录树(与委托的区别、自行拼接、总结几类实例间的拷贝)

    一. 基本介绍 回忆: 最早接触到表达式目录树(Expression)可能要追溯到几年前使用EF早期的时候,发现where方法里的参数是Expression<Func<T,bool> ...

  9. 使用Java函数接口及lambda表达式隔离和模拟外部依赖更容易滴单测

    概述 单测是提升软件质量的有力手段.然而,由于编程语言上的支持不力,以及一些不好的编程习惯,导致编写单测很困难. 最容易理解最容易编写的单测,莫过于独立函数的单测.所谓独立函数,就是只依赖于传入的参数 ...

随机推荐

  1. android摄像头获取图像——第三弹

    相机获取图像的格式问题 android中承认的格式的参考网址为 :http://developer.android.com/reference/android/graphics/ImageFormat ...

  2. 网络工程师HCNA认证学习笔记Day1

    企业网络 企业网络远程互联是广域网WAN互联,而非互联网Internet小型企业网络:一个路由器.交换机.AP大型企业网络:核心层.汇聚层.接入层.考虑可用性.稳定性.扩展性.安全性.可管理,冗余. ...

  3. 给 UILabel 中的文字增加 line-through / Strikethrough (删除线)样式

    iOS 6 中苹果引入了 NSStrikethroughStyleAttributeName 属性,用于设置 NSAttributedString 的删除线样式,用法如下: let attribute ...

  4. spark sql 优化心得

    本篇文章主要记录最近在使用spark sql 时遇到的问题已经使用心得. 1 spark 2.0.1 中,启动thriftserver 或者是spark-sql时,如果希望spark-sql run ...

  5. GPDB外部表创建示例

    创建以|为分隔符的外部表CREATE EXTERNAL TABLE ext_expenses ( name text,date date, amount float4, category text, ...

  6. 初学Java web(转)

    转自 http://www.oschina.net/question/12_52027 OSCHINA 软件库有一个分类——Web框架,该分类中包含多种编程语言的将近500个项目. Web框架是开发者 ...

  7. DBUtils学习一 增删该查

    package com.mozq.jdbc.test; import java.sql.SQLException; import java.util.List; import java.util.Ma ...

  8. 洛谷1005(dp)

    1.不要贪,缩小区间去dp就好. 2.预处理指数. 3.__int128可还行. #include <cstdio> #include <cctype> #include &l ...

  9. Gym - 101810E ACM International Collegiate Programming Contest (2018)

    bryce1010模板 http://codeforces.com/gym/101810 #include<bits/stdc++.h> using namespace std; #def ...

  10. linux进行文件vim编辑时没有退出文件直接关闭出现E325: ATTENTION Found a swap file by the name "/usr/local/php/etc/.php.ini.swp"

    E325: ATTENTIONFound a swap file by the name "/usr/local/php/etc/.php.ini.swp"          ow ...