Luo Gu P1067 polynomial output

..

Title Description

One yuan n- n-degree polynomial expression as expressed available:

f(x)=a_nx^n+a_{n-1}x^{n-1}+\cdots +a_1x+a_0,a_n\ne 0f(x)=anxn+an1xn1++a1x+a0,an0

Wherein, a_ix ^ i A i X i called i i-th order term, a_i A i  called i coefficients of the i-th term. Gives a univariate polynomial and the number of multipliers, the required output of the polynomial specified in the following format:

  1. It is a polynomial argument X X, from left to right in descending frequency order polynomial is given.

  2. Contains only the polynomial coefficients is not 0 0 entries.

  3. If the polynomial the n- the n-order term coefficient is positive, then the polynomial is not the beginning of a "+" sign, if the polynomial the n- the n-order term coefficient is negative, the polynomial a "-" sign at the beginning.

  4. For not the highest order terms to the "+" or "-" sign this connection with the previous item, respectively, this coefficient is positive or negative coefficient. Followed by a positive integer that represents the absolute value of this coefficient (if a higher than 0 the zero-order term, the absolute value of the coefficient . 1 1, the output is not necessary  . 1 1). If X X greater than the index . 1 1, the exponential portion immediately next to " X ^ B X B", wherein  B B is  X X index; if  X X's index . 1 1, the next immediately the exponent part with the form " X X"; if  X X index of 0 to 0, the coefficients can only output.

  5. Polynomials, polynomial beginning, the end, without extra spaces.

Input and output formats

Input formats:

 

Enter the total of  2 2 lines

The first line . 1 an integer, n- n-, the number of univariate polynomial representation.

The second row has  n-1 + n- + 1 integers, the first of which I I denotes the integer n-1 + I- n- - I + coefficient of the linear term, between each two integers separated by a space.

 

Output formats:

 

Total output  . 1 one line, the output of the polynomial by topic format.

 

Sample input and output

Input Sample # 1:  Copy
5 
100 -1 1 -3 0 10
Output Sample # 1:  Copy
100x^5-x^4+x^3-3x^2+10
Input Sample # 2:  Copy
3 
-50 0 0 1 
Output Sample # 2:  Copy
-50x^3+1 

Explanation

NOIP 2009 universal set of the first question

Data for 100%, 0 \ n-Le \ Le 100 0 n- . 1 0 0, -100 \ Le - . 1 0 0 ≤ coefficient \ 100 Le . 1 0 0

..

 

 

Solution: This problem is really mouth area Heart. . .

First thought is simple. Later, from 50> 40--40--40 points, the last breath of the AC.

Knock yourself debugging data to several 01,011,100 and the like.

The second question looked explanation because it is a reciprocal square so it knows to special treatment orz

And, more pit, it may not necessarily be the first time the highest no sign, but the first time that a no plus sign, so use nosign variable records whether a first item, and if the whole It is zero, but also special sentenced output 0.

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cmath>
 4 #include <stdio.h>
 5 #include <cstring>
 6 #include <string>
 7 #include <cstdlib>
 8 #include <queue>
 9 #include <stack>
10 #include <set>
11 #include <vector>
12 #include <map>
13 #include <list>
14 #include <iomanip>
15 #define maxn 10000
16 //#include <>
17 using namespace std;
18 int main()
19 {
20     int n,x,sign=0,nosign=0;
21     int last=0;
22     scanf("%d",&n);
23     for(int i=n;i>=0;i--)
24     {
25         nosign=0;
26         scanf("%d",&x);
27         if(i==n)
28              Last = X;
 29          the else {
 30              IF (Last == 0 )
 31 is                  nosign = . 1 ;
 32              Last = . 1 ;
 33 is          }
 34 is          IF (X == 0 )
 35              {
 36                  Sign ++ ;
 37 [                  Continue ;
 38 is              }
 39          // head unit: no header symbols, and several square direct output x 
40          IF (I == n-) {
 41 is              IF (x == . 1== X || - . 1 )
 42 is              {
 43 is                  IF (X < 0 )
 44 is                      the printf ( " - " );
 45                  the printf ( " X% ^ D " , I);
 46 is              }
 47              the else {
 48                  the printf ( " % ^ DX D% " , X, I);
 49              }    
 50              
51 is          }
 52 is          // penultimate 
53 is          the else  IF (I == . 1 )
 54 is         {
55             if(x==1||x==-1)
56             {
57                 if(x<0&&!nosign)
58                     printf("-");
59                 else if(x>0&&!nosign){
60                     printf("+");
61                 }
62                 printf("x");
63             }
64             else{
65                 if(x>0&&!nosign)
66                     printf("+");
67                 printf("%dx",x);
68             }
69         } 
      //中部
70 else if(i!=0){ 71 if(x>0&&!nosign) 72 printf("+"); 73 if(x==1||x==-1) 74 { 75 IF (X < 0 ) 76 the printf ( " - " ); 77 the printf ( " X ^% D " , I); 78 } 79 the else { 80 the printf ( " % DX ^% D " , X, I); 81 } 82 83 } 84 // tail: output x. 85 the else { 86 IF (X> 0 &&! Nosign) 87 printf("+"); 88 printf("%d",x); 89 } 90 } 91 if(sign==n) 92 cout<<"0"; 93 printf("\n"); 94 return 0; 95 }

 

Guess you like

Origin www.cnblogs.com/greenaway07/p/10969411.html