< span > https://ac.nowcoder.com/acm/contest/49417/F < / span > < / a > < br / > source: cattle from
What cases are not considered? Always show pass rate 90%
polynomial of degree n in one variable can be expressed as follows:
f(x) = anxn+ an-1xn-1 + ... + a1x + a0, a0≠0
Where, aixi is called the term i, and ai is called the coefficient of the term i. Give the degree and coefficient of a polynomial in one variable, please output the polynomial in the following format:
The independent variable inpolynomial is x, and the polynomial is given in decreasing order of degree from left to right.
polynomials contain only terms whose coefficients are not zero.
No "+" sign appears at the beginning of the polynomial if the coefficient of the NTH degree is positive, and the polynomial begins with a "-" sign if the coefficient of the NTH degree is negative.
For the item that is not the highest order, connect this item with the previous item with a + sign or a - sign to indicate that the coefficient of this item is positive or negative respectively. This is followed by a positive integer indicating the absolute value of the coefficient(if an item greater than 0 has an absolute value of 1 for its coefficient, there is no need to output 1). If the exponent of x is greater than 1, then the exponent part that follows is of the form "x^b", where b is the exponent of x; If x has an exponent of 1, then the exponent that follows is of the form "x"; If the exponent of x is 0, then you just output the coefficient.
In polynomials, the beginning and end of polynomials do not contain unnecessary Spaces.
#include<iostream>
using namespace std;
#define max 101
int main(){
int n,a[max];
cin>>n;
for(int i=1;i<n+2;i++){
cin>>a[i];
}
if(a[1]>0&&a[1]!=1){
cout<<a[1]<<"x^"<<n;
}
if(a[1]<0&&a[1]!=-1){
cout<<a[1]<<"x^"<<n;
}
if(a[1]==1){
cout<<"x^"<<n;
}
if(a[1]==-1){
cout<<"-x^"<<n;
}
if(a[1]==0){}
for(int i=2;i<n;i++){
if(a[i]>0){
if(a[i]==1){cout<<"+x^"<<n+1-i;}
else{cout<<"+"<<a[i]<<"x^"<<n+1-i;}
}
else if(a[i]<0){
if(a[i]==-1){cout<<"-x^"<<n+1-i;}
else{cout<<a[i]<<"x^"<<n+1-i;}
}
else{}
}
if(a[n]==0){}
else if(a[n]>0){cout<<"+"<<a[n]<<"x";}
else{cout<<a[n]<<"x";}
if(a[n+1]==0){}
else if(a[n+1]>0){cout<<"+"<<a[n+1]<<endl;}
else{cout<<a[n+1]<<endl;}
}
0 Answer
No answer yet
这家伙很懒,什么都没留下...