『题解』A4.多项式输出
2025-06-07 21:10:48
发布于:湖南
0阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define re return
#define ew 5005
#define sz 10000005
#define sscc cout<<6;
const int MOD=1e9+7;
int a[sz];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin>>n;
	if(n==0){
		int x;
		cin>>x;
		cout<<x;
		re 0;
	}
	for(int i=n+1;i>=1;i--){
		cin>>a[i];
		//cout<<i<<" "<<a[i]<<endl;
		if(i==1){
			if(a[i]>0){
				cout<<"+"<<a[i];
			}
			else if(a[i]<0){
				cout<<a[i];
			}
		} 
		else if(a[i]!=0){
			if(a[i]>0){
				if(i!=n+1){
					cout<<"+";
				}
				if(a[i]==1){
					if(i==2)
						cout<<"x";   
					else 
						cout<<"x^"<<i-1;
				}
				else{
					if(i==2)
						cout<<a[i]<<"x";   
					else 
					cout<<a[i]<<"x^"<<i-1;
				}
			}
			else{
				if(a[i]==-1){
					if(i==2)
						cout<<"-x";   
					else 
						cout<<"-x^"<<i-1;
				}
				else{
					if(i==2)
						cout<<a[i]<<"x";   
					else 
						cout<<a[i]<<"x^"<<i-1;
				}
			}
		}
	}
	re 0;
}
这里空空如也







有帮助,赞一个