Files
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

35 lines
1.0 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
/*
从前往后依次处理每一项,依次考虑符号、系数、x、x的次数:
1、如果系数是0,则直接continue;
2、如果不是第一个非零项,且系数是正的,则输出'+';如果系数是负的,则无条件输出'-';
3、如果系数的绝对值不是1,或者是常数项,则输出系数的绝对值;
4、如果次数不为0,则输出x
5、如果次数大于1,则输出次数
*/
int main() {
int n;
cin >> n; // 一元多项式的次数
bool is_first = true;
for (int i = n; i >= 0; i--) { // 其中第 i 个整数表示第 n−i+1 次项的系数
int a;
cin >> a;
if (!a) continue;
if (!is_first && a > 0)
printf("+");
else if (a < 0)
printf("-");
if (abs(a) != 1 || i == 0) printf("%d", abs(a));
if (i) printf("x");
if (i > 1) printf("^%d", i);
is_first = false;
}
return 0;
}