Rokua-P1067 polynomial output
--Author: Shore Zhi-ting LAN
first, the topic
Title Description
The unary n-th polynomial can be expressed as follows:
Where Aixi is called i-th term, the AI is called the coefficient of i-th term. Given the number of times and coefficients for a unary polynomial, output the polynomial in the format required as follows:
In the polynomial, the independent variable is x, and the polynomial is given in descending order of the number of times from left to right.
Only items with a factor not 0 are included in the polynomial.
If the polynomial n coefficient is positive, the polynomial begins with a "+" sign, and if the polynomial n coefficient is negative, the polynomial begins with the "-" sign.
For items that are not the highest, the "+" or "-" numbers are concatenated with the previous item, respectively, to indicate that the coefficient is positive or negative. Immediately follows a positive integer that represents the absolute value of this coefficient (if an item is above 0, the absolute value of the coefficient is 1, no output 1 is required). If the exponent of x is greater than 1, the next exponential part is "X^b", where B is the exponent of X, and if the exponent of X is 1, then the exponential part that follows is "X", and if the exponent of x is 0, only the output factor is required.
In a polynomial, the beginning and end of a polynomial does not contain extra spaces. input/output format Input Format:
Enter a total of 2 lines
The first row of 1 integers, n, represents the number of times a unary polynomial is.
The second line has n+1 integers, where the i integer represents the coefficients of the n-i+1, separated by a space between every two integers. output Format:
Output a total of 1 lines, according to the format of the topic output polynomial. input/Output sample Input Sample # #:
5
100-1 1-3 0 Input Example # #:
3
-50 0 0 1 Sample # # of outputs:
100x^5-x^4+x^3-3x^2+10 Output Example #:
-50x^3+1 Description
NOIP 2009 Universal group first question
For 100% data, 0<=n<=100,-100<= coefficient <=100 second, thinking
It was very embarrassing, I submitted four times before AC, so I decided to write a puzzle.
Talk less and get to the chase.
A typical simulation problem, you can read the side output. More pits, list: the coefficient of 1 (more than a few special) coefficient of 1 (ibid.) The first few coefficients of 0 (can be resolved with a bool variable mark) Number of 1, 0 (special) cycle times (n+1)
Revelation: Make a few more sets of data on the field to avoid stepping on the pits. third, my C + + source code:
Luogu P1067 #include <iostream> #include <cstdio> typedef long Long LL;
using namespace Std;
inline int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |
Ch> ' 9 ') {if (ch== '-') f=-1;
Ch=getchar ();
} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 ';
Ch=getchar ();
} return x*f;
} const int maxn=105;
int n; BOOL Flag;
Judge whether the first item int A[MAXN];
int main () {//freopen ("In.txt", "R", stdin);
Freopen ("OUT.txt", "w", stdout);
N=read ();
for (int i=1;i<=n+1;i++) {//Note the number of cycles a[i]=read ();
if (a[i]==0) {continue;
} if (n-i+1==0) {//index is 0 if (a[i]>0&&flag) printf ("+%d", A[i]);
if (a[i]<0&&flag) printf ("%d", a[i]);
if (!flag) printf ("%d", a[i]);
Continue
} if (n-i+1==1) {if (A[i]>0&&flag) {if (a[i]!=1) printf ("+%dx", A[i]);
else{ printf ("+x");
}} if (A[i]<0&&flag) {if (a[i]!=-1) printf ("%dx", A[i]);
else{printf ("-X", n-i+1);
}} flag=true;
Continue
} if ((!flag) &&a[i]>0) {if (a[i]!=1) printf ("%dx^%d", a[i],n-i+1);
if (a[i]==1) {printf ("x^%d", n-i+1);
} flag=true;
Continue
} if (a[i]>0) {if (A[i]!=1&&flag) printf ("+%dx^%d", a[i],n-i+1);
if (flag&&a[i]==1) {printf ("+x^%d", n-i+1);
} if (!flag) printf ("%dx^%d", a[i],n-i+1);
} else{if (a[i]!=-1) printf ("%dx^%d", a[i],n-i+1);
else{printf ("-x^%d", n-i+1);
}} flag=true;
}//fclose (stdin); fclose (stdout);
return 0; }
Do not CTRL + C OH.