POJ 1472

#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <cstring>

using namespace std;

int string_to_int(const string &string1) {
    istringstream iss(string1);
    int a;
    iss>>a;
    return a;
}

bool solve(int *a) {
    string s;
    cin >> s;
    if (s[0] == 'B') {
        while (solve(a));
        return true;
    } else if (s[0] == 'L') {
        string t;
        cin >> t;
        int temp[11] = {0};
        while (solve(temp));
        if (t == "n") {
            for (int i = 9; i >= 0; --i) {
                temp[i + 1] = temp[i];
            }
            temp[0] = 0;
        } else {
            for (int i = 0; i < 11; ++i) {
                temp[i] *= string_to_int(t);
            }
        }
        for (int i = 0; i < 11; ++i) {
            a[i] += temp[i];
        }
        return true;
    } else if (s[0] == 'O') {
        int num;
        cin >> num;
        a[0] += num;
        return solve(a);
    } else if (s[0] == 'E')
        return false;
}
void display(int * exp){
    bool isfirst = true;
    bool has = false;
    for (int i = 10 ;i >=1 ;i--){
        if (exp[i]){
            has = true;
            if (isfirst){
                if (exp[i]>1)
                    cout<<exp[i]<<"*";
                if (i>1)
                    cout<<"n^"<<i;
                else
                    cout<<"n";
                isfirst = false;
            } else{
                cout<<"+";
                if (exp[i]>1)
                    cout<<exp[i]<<"*";
                if (i>1)
                    cout<<"n^"<<i;
                else
                    cout<<"n";
            }
        }
    }
    if (exp[0]){
        if (!isfirst){
            cout<<"+"<<exp[0];
        } else{
            cout<<exp[0];
        }
    } else{
        if (!has)
            cout<<0;
    }
}

int main() {
//    freopen("../in.txt", "r", stdin);
    int coff[11] = {0};
    int testcase;
    cin>>testcase;
    for (int t = 1; t <= testcase; ++t) {
        memset(coff, 0, sizeof(coff));
        solve(coff);
        printf("Program #%d\n"
               "Runtime = ",t);
        display(coff);
        cout<<endl<<endl;
    }
}

Reference

https://blog.csdn.net/lyy289065406/article/details/6648640

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值