把每个数字都拆开来看
拆开来有1.正负号2.数字的值(绝对值,因为与符号拆开来看)3.输出的x^b。我的三个函数分别实现了这个功能,你看看,比较容易理解,
st是判断是不是第一次输出,如果第一次输出的话,正号就不需要了
```
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
bool st;
int n;
void f1(int x,int i)
{
if(st)
{
if(x > 0) cout<<"+";
if(x < 0) cout<<"-";
}
else
{
if(x < 0) cout<<"-";
}
}
void f2(int x,int i)
{
if(i == 0) cout<<abs(x);
else
{
if(abs(x) != 1) cout<<abs(x);
}
}
void f3(int x,int i)
{
if(i == 0) return;
if(i == 1) cout<<"x";
if(i > 1) cout<<"x^"<<i;
}
int main()
{
cin>>n;
for(int i = n;i >= 0;i --)
{
int x;
cin>>x;
if(x)
{
f1(x,i);
f2(x,i);
f3(x,i);
st = true;
}
}
}
```
by dTvTb @ 2023-01-04 17:25:16