POJ 1555 Polynomial Showdown

题目:POJ 1555

Describe:
Given the coefficients of a polynomial from degree 8 down to 0, you are to format the polynomial in a readable format with unnecessary characters removed. For instance, given the coefficients 0, 0, 0, 1, 22, -333, 0, 1, and -1, you should generate an output line which displays x^5 + 22x^4 - 333x^3 + x - 1.
The formatting rules which must be adhered to are as follows:

1. Terms must appear in decreasing order of degree.

2. Exponents should appear after a caret `"^".

3. The constant term appears as only the constant.

4. Only terms with nonzero coefficients should appear, unless all terms have zero coefficients in which case the constant term should appear.

5. The only spaces should be a single space on either side of the binary + and - operators.

6. If the leading term is positive then no sign should precede it; a negative leading term should be preceded by a minus sign, as in -7x^2 + 30x + 66.

7. Negated terms should appear as a subtracted unnegated term (with the exception of a negative leading term which should appear as described above). That is, rather than x^2 + -3x, the output should be x^2 - 3x.

8. The constants 1 and -1 should appear only as the constant term. That is, rather than -1x^3 + 1x^2 + 3x^1 - 1, the output should appear as-x^3 + x^2 + 3x - 1.
Input:
The input will contain one or more lines of coefficients delimited by one or more spaces. There are nine coefficients per line, each coefficient being an integer with a magnitude of less than 1000.
Output:
The output should contain the formatted polynomials, one per line.
Sample Input:
0 0 0 1 22 -333 0 1 -1
0 0 0 0 0 0 -55 5 0
Sample Output:
x^5 + 22x^4 - 333x^3 + x - 1
-55x^2 + 5x

题目大意:

有多个测试样例,每个样例九个不大于1000的整数,每个整数表示多项式 指数为8-0项的系数,要求,据此输出合法的多项式,要求:

1、系数为1,则不打印1,为-1,则只打印负号

2、运算符与运算数之间要有空格

3、若所有系数都为0,则打印一个0

4、指数顺序必须是由高到低

5、指数必须出现在‘ ^ ’ 符号之后

6、系数为0的项不需要输出

大概就是这样。

题目解析:

思路很简单,就是模拟,但是考虑的条件非常多,需要仔细考虑,不遗漏细节,主要按照该项是否为首项来划分,每个划分里在分为,指数为1,为0等情况来特判。

AC代码:

 

  1 #include 
  2 #include 
  3 using namespace std;
  4 int a[9]; //用来存储系数的数组
  5 int main()
  6 {
  7     // 这里有点奇怪,遍历输入好像不行,只能硬输入,还好系数比较少,暂时不知道为什么
  8     while(~scanf("%d%d%d%d%d%d%d%d%d",&a[8],&a[7],&a[6],&a[5],&a[4],&a[3],&a[2],&a[1],&a[0]))
  9     {
 10         int f = 0; // 这个用来标志是否为首项
 11         int flag = 0; // 这个变量用来标志是否系数都为0
 12         for(int i = 0; i < 9; i++) // 遍历一遍,查看是否都为0
 13         {
 14             if(a[i] != 0)
 15             {
 16                 flag = 1;
 17                 break;
 18             }
 19         }
 20         if(flag == 0) {printf("0\n");continue;} // 若都为零,直接输出0
 21         for(int i = 8; i >= 0; i--)  // 否则,遍历每一个指数
 22         {
 23             if(f == 0) // 第一种情况,首项
 24             {
 25                 if(a[i] == 0) continue;
 26                 else if(i == 1)
 27                 {
 28                     if(a[i] == 1)
 29                     {
 30                         printf("x");
 31                         f = 1;
 32                         continue;
 33                     } else if(a[i] == -1) {
 34                         printf("-x");
 35                         f = 1;
 36                         continue;
 37                     } else {
 38                         printf("%dx",a[i]);
 39                         f = 1;
 40                         continue;
 41                     }
 42                 } else if(i == 0) {
 43                     printf("%d",a[i]);
 44                     f = 1;
 45                     continue;
 46                 } else {
 47                     if(a[i] == -1) {
 48                         printf("-x^%d",i);
 49                         f = 1;
 50                         continue;
 51                     } else if(a[i] == 1) {
 52                         printf("x^%d",i);
 53                         f = 1;
 54                         continue;
 55                     } else {
 56                         printf("%dx^%d",a[i],i);
 57                         f = 1;
 58                         continue;
 59                     }
 60                 }
 61             } else {        // 第二种情况,非首项
 62                 if(a[i] == 0) continue;
 63                 else if(i == 1) {
 64                     if(a[i] == 1) {
 65                         printf(" + x");
 66                         continue;
 67                     } else if(a[i] == -1) {
 68                         printf(" - x");
 69                         continue;
 70                     } else if(a[i] > 0) {
 71                         printf(" + %dx",a[i]);
 72                         continue;
 73                     } else if(a[i] < 0) {
 74                         printf(" - %dx",-a[i]);
 75                         continue;
 76                     }
 77                 } else if(i == 0) {
 78                     if(a[i] > 0) printf(" + %d",a[i]);
 79                     else if(a[i] < 0) printf(" - %d",-a[i]);
 80                 } else {
 81                     if(a[i] == 1) {
 82                         printf(" + x^%d",i);
 83                         continue;
 84                     } else if(a[i] == -1) {
 85                         printf(" - x^%d",i);
 86                         continue;
 87                     } else if(a[i] > 0) {
 88                         printf(" + %dx^%d",a[i],i);
 89                         continue;
 90                     } else if(a[i] < 0) {
 91                         printf(" - %dx^%d",-a[i],i);
 92                         continue;
 93                     }
 94                 }
 95             }
 96         }
 97         printf("\n"); // 注意换行
 98     }
 99     return 0;
100 }
View Code

 

小结:

多项式存储有两种方法,各有优劣:

1、用一维数组,存储各项系数,下标即为指数,长度为指数最大的数。

2、用结构体数组,里面存系数和下标,长度为非零项个数。

 

你可能感兴趣的:(POJ 1555 Polynomial Showdown)