[题解] 多项式还原

· · 个人记录

看代码吧,很好理解

#include <bits/stdc++.h>
using namespace std;
int n, m, k, a[100], i = 1, t = 0;
int main(){
    cin >> n >> m;
    k = n + 1;
    while(m > 0){
        a[i] = m % k;
        if(a[i] != 0 && t == 0) t = i;
        m /= k;
        i++;
    }
    for(int j = i; j > t; j--){
        if(a[j] != 0 && a[j] != 1 && j - 1 != 1){
            cout << a[j] << "x^" << j - 1 << "+";
        }else if(a[j] != 0 && a[j] == 1 && j - 1 != 1){
            cout << "x^" << j - 1 << "+";
        }else if(a[j] != 0 && a[j] != 1 && j - 1 == 1){
            cout << a[j] << "x" << "+";
        }else if(a[j] != 0 && a[j] == 1 && j - 1 == 1){
            cout << "x" << "+";
        }
    }
    if(a[t] != 1 && t - 1 > 1){
        cout << a[t] << "x^" << t - 1;
    }else if(a[t] == 1 && t - 1 > 1){
        cout << "x^" << t;
    }else if(a[t] != 1 && t - 1 == 1){
        cout << a[t] << "x";
    }else if(a[t] == 1 && t - 1 == 1){
        cout << "x";
    }else if(t - 1 == 0){
        cout << a[t];
    }
    return 0;
}