AcWing 437. 多项式输出
原题链接
简单
作者:
HiCode_001
,
2019-11-06 01:39:16
,
所有人可见
,
阅读 691
题意理解:
让我们根据多项式系数,输出指定的多项式
考察知识点:
字符串处理、模拟
时间复杂度 O(n)
思路分析:
1)从最高位到最低位分析
2)如果系数为0,就不用输出相应的多项式
3)如果系数为正,且不为开头第一位,就输出"+",如果为负就输出"-"
4)系数不为1,或是为最后一个0次幂的多项式,就输出系数的绝对值
5)次数不为0,则输出x
6)次数大于1,则输出次数
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 105;
int a;
bool first = true;
int main(){
int n;
cin >> n;
for(int i = n; i>=0; i--){
cin >> a;
if(!a) continue;
if(!first&&a>0) cout << "+";
else if(a<0) cout << "-";
if(abs(a)!=1||!i) cout << abs(a);
if(i) cout << "x";
if(i>1) cout << "^" << i;
first = false;
}
}