FZU 2215 Simple Polynomial Problem【模拟】【表达式计算】

题目链接

http://acm.fzu.edu.cn/problem.php?pid=2215

思路

题意就是叫你把给你的多项式化到最简,输出各项系数。

表达式计算用栈实现就行了,但这题需要改动下,数据栈内不能直接存数字了,而要存多项式,定义一个数组c[i]表示 xi 的系数,然后自己实现下加法乘法即可。

AC代码

#include <iostream>
#include <cmath>
#include <stack>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

const int MOD = 1000000007;
struct polynomial
{
    ll c[600];
    int n;
    polynomial()
    {
        n = 0;
        memset(c, 0, sizeof c);
    }
    friend polynomial operator + (polynomial a, polynomial b)
    {
        polynomial ans;
        int len = max(a.n, b.n);
        for (int i = 0; i <= len; ++i)
        {
            ans.c[i] = a.c[i] + b.c[i];
            ans.c[i] %= MOD;
        }
        ans.n = len;
        return ans;
    }
    friend polynomial operator * (polynomial a, polynomial b)
    {
        polynomial ans;
        if (a.c[a.n] == 0 || b.c[b.n] == 0)
            return ans;
        for (int i = 0; i <= a.n; ++i)
        {
            if (i != 0)//每次向右移动一位
            {
                for (int j = b.n + 1; j >= 1; --j)
                {
                    b.c[j] = b.c[j - 1];
                }
                b.c[0] = 0;
                b.n++;
            }
            polynomial t = b;
            for (int j = 0; j <= b.n; ++j)
            {
                t.c[j] *= a.c[i];
                t.c[j] %= MOD;
            }
            ans = ans + t;
        }
        return ans;
    }
};

stack <polynomial> st_num;
stack <char> st_op;
void calAB(char op)
{
    if (op == '+')
    {
        st_op.pop();
        polynomial a, b;
        a = st_num.top(); st_num.pop();
        b = st_num.top(); st_num.pop();
        st_num.push(a + b);
    }
    else if (op == '*')
    {
        st_op.pop();
        polynomial a, b;
        a = st_num.top(); st_num.pop();
        b = st_num.top(); st_num.pop();
        st_num.push(a * b);
    }
}
polynomial calculate(string s)
{
    for (int i = 0; i < s.length(); ++i)
    {
        if (s[i] == '(' || s[i] == '*')
        {
            st_op.push(s[i]);
        }
        else if (s[i] == '+')
        {
            while (!st_op.empty())
            {
                if (st_op.top() == '*')
                {
                    calAB('*');
                }
                else break;
            }
            st_op.push(s[i]);
        }
        else if (s[i] == ')')
        {
            while (!st_op.empty())
            {
                if (st_op.top() == '*')
                {
                    calAB('*');
                }
                else if (st_op.top() == '+')
                {
                    calAB('+');
                }
                else if (st_op.top() == '(')
                {
                    st_op.pop();
                    break;
                }
            }
        }
        else if (isdigit(s[i]))
        {
            polynomial t;
            t.n = 0;
            t.c[0] = s[i] - '0';
            st_num.push(t);
        }
        else if (s[i] == 'x')
        {
            polynomial t;
            t.n = 1;
            t.c[1] = 1;
            st_num.push(t);
        }
    }
    while (!st_op.empty())//最后清算
    {
        if (st_op.top() == '*')
        {
            calAB('*');
        }
        else if (st_op.top() == '+')
        {
            calAB('+');
        }
    }
    polynomial ans = st_num.top();
    while (!st_num.empty())st_num.pop();
    while (!st_op.empty())st_op.pop();
    return ans;
}
int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        string s;
        cin >> s;
        polynomial ans = calculate(s);
        bool flag = 0;
        for (int i = ans.n; i >= 0; --i)
        {
            if (i == ans.n)printf("%lld", ans.c[i]);
            else printf(" %lld", ans.c[i]);
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值