跳到正文

个人记录

多项式还原-题解

SpriteQAQ约 2 分钟
原文

这是 2026年7月9日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2024年8月23日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

看代码吧,很好理解

C++
#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;
}