You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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 个整数表示第 ni+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;
}