- 题解
DAY4多项式输出题解
- @ 2024-8-8 18:25:41
题意解析
输入一个多项式的各项系数,输出这个多项式。
思路解析
循环输入各项系数,依次解析每一种情况:
1.全为1以上
2.包含0,1。
3.包含负数。
4.第一项为负数。
5.常数项为零。
用if判断分情况输出。
错因:忽略了常数项为零的情况。
#include<bits/stdc++.h>
using namespace std;
long long n,x;
int main(){
//freopen("poly.in","r",stdin);
//freopen("poly.out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++){
cin>>x;
if(x!=0){
if(i!=1){
if(x>0) cout<<"+";
}
if(x==-1)cout<<"-";
if(x!=1 and x!=-1)cout<<x;
cout<<"x";
if(i!=n)cout<<"^"<<n-i+1;
}
}
cin>>x;
if(x!=0){
if(x>0 and n>0)cout<<"+";
cout<<x;
}
}
0 条评论
目前还没有评论...