uva1661方程——建树模拟

摘自:https://www.cnblogs.com/jerryRey/p/4769144.html

题意:

解一个一元一次方程

解决

就是实现比较麻烦,记录一下。

#include <cstdio>
#include <stack>
#include <vector>
#include <cstring>
#include <algorithm>
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
const int maxn = 600+5;

bool isOp[256]; 
char rev[256];

LL Gcd(LL a, LL b){return b == 0 ? a : Gcd(b, a%b);}
// 分数 
struct Fraction{
	LL p,q;	// p/q
	Fraction(LL a = 0, LL b = 1):p(a),q(b){ simplify(p,q); }
	void simplify(LL& p, LL& q){ LL c = Gcd(p,q); p /= c; q /= c; }  // 化简 
	Fraction operator = (int a){p = a; q = 1; return (*this);}
	Fraction operator = (LL a){p = a; q = 1; return (*this);}
	Fraction operator - (){ return Fraction(-p,q); }
	Fraction operator + (Fraction& x){
		LL a,b;
		a = p*x.q + q*x.p;
		b = q * x.q;
		return Fraction(a,b);
	}
	Fraction operator += (Fraction& x){ return (*this) = (*this) + x; }
	Fraction operator - (Fraction& x){ return (-x) + (*this); }
	Fraction operator -= (Fraction& x){ return (*this) = (*this) - x; }
	Fraction operator * (Fraction& x){
		LL a,b;
		a = p * x.p;
		b = q * x.q;
		return Fraction(a,b); 
	}
	Fraction operator *= (Fraction& x){ return (*this) = (*this) * x; }
	Fraction operator / (Fraction& x){ return Fraction(x.q,x.p) * (*this); }
	Fraction operator /= (Fraction& x){ return (*this) = (*this)/x; }
	bool operator == (const Fraction& x){ return p*x.q == q*x.p; }
	bool operator == (const int x){ return p == x * q; }
	bool operator < (Fraction& x){ return p*x.q < q*x.p; }
	void print(){ simplify(p,q); if(q < 0) q = -q,p = -p; printf("%lld/%lld\n",p,q); }
};
Fraction ans;

// 二叉树节点 
struct Node{
	Node *l, *r;// 左右孩子 
	Fraction f; // 分数
	char op;	// 操作符 
	bool is_x;	// 是不是x 
	Node(){};
	Node(Fraction& c, Node* a = NULL, Node* b = NULL):f(c),l(a),r(b){};
}nodes[maxn];

Fraction calc(Fraction& a, Fraction& b, char op){
	switch(op){
		case '+': return a+b;
		case '-': return a-b;
		case '*': return a*b;
		case '/': return a/b;
	}
	return Fraction(999,1);
}

// 返回根节点指针 
Node* input(char ch){
	int cnt = 0;
	stack<Node*> S;
	do{
		while(ch == ' ') ch = getchar();
		Node& cur = nodes[cnt];
		if(isOp[ch]){		//是操作符 
			cur.op = ch;
			cur.r = S.top(); S.pop();
			cur.l = S.top(); S.pop();
			cur.is_x = cur.l->is_x||cur.r->is_x;
			if(cur.is_x){ 	//子树里有x
				//处理系数为0的情况
				if((cur.op == '*'&&(cur.l->is_x ? cur.r->f == 0 : cur.l->f == 0))||(cur.op == '/'&&cur.r->is_x&&cur.l->f == 0)){
					cur.is_x = false;
					cur.f = 0; cur.l = cur.r = NULL;
				}
			}
			else{			// 算出结果,左右孩子置空,当做叶节点处理 
				cur.f = calc(cur.l->f, cur.r->f, cur.op);
				cur.l = cur.r = NULL;
			}
		}
		else{				// 是操作数 
			cur.l = cur.r = NULL;
			if(ch == 'X') cur.is_x = true;
			else{
				cur.is_x = false;
				int num = ch - '0';
				while((ch = getchar())&&ch >= '0'&&ch <= '9') num = num*10 + ch-'0';
				cur.f = num;
			}
		}
		S.push(nodes+cnt);
		ch = getchar(); ++cnt;
	}while(ch != '\n'&&~ch);
	return S.top();
}

void calcRev(Fraction& f, char op){
	switch(op){
		case '+': ans = ans - f; break;// f + (fx) = ans
		case '-': ans = f - ans; break;// f - (fx) = ans
		case '*': ans = ans / f; break;// f * (fx) = ans
		case '/': ans = f / ans; // f / (fx) = ans
	}
}
bool dfs(Node* root){
	// 叶子节点
	if(root->l == NULL) return true; 
	// x在左子树
	if(root->l->is_x){
		ans = calc(ans, root->r->f, rev[root->op]);
		if(!dfs(root->l)) return false;
	} 
	else if(root->r->is_x){
		calcRev(root->l->f, root->op);
		if(ans.q == 0) return false;
		if(!dfs(root->r)) return false;
	}
	return true;
}

int main()
{
	freopen("in.txt","r",stdin);
	memset(isOp, 0, sizeof(isOp));
	isOp['+'] = isOp['-'] = isOp['*'] = isOp['/'] = true;
    rev['+'] = '-'; rev['-'] = '+'; rev['*'] = '/'; rev['/'] = '*';
	char ch;
	while(~(ch = getchar())){
		Node* root = input(ch);
		if(!root->is_x){
			if(root->f == 0) printf("MULTIPLE\n");
			else printf("NONE\n");
			continue;
		}
		ans = 0;
		if(dfs(root)) { printf("X = "); ans.print(); }
		else{ printf("NONE\n"); }
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值