多项式存储的两种方式:
1、数值数组 数组的下标与多项式的项的指数相关,数组长度取决于多项式的最高次幂。
2、结构数组 数组下标为项序号而非指数值,a[i].coef 当前项的系数 以及a[i].exp 当前项的指数。数组长度是多项式的实际长度。
ZOJ 1720
Polynomial Showdown
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 file 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 file 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
c:
#include<stdio.h>
int main(){
int num[9]={0};
int m=0;
while(scanf("%d",&m)!=EOF){
int i=0;
num[0]=m;
for(i=1;i<9;i++){
scanf("%d",&num[i]);
}
int countZ=0,countN=0;
for(i=0;i<9;i++){
if(num[i]==0){
countZ++;
}else{
countN++;
if(i==7){//x项
if(countN!=1){
if(num[i]==1){
printf(" + ");
printf("x");
}else if(num[i]==-1){
printf(" - ");
printf("x");
}else{
if(num[i]>0){
printf(" + ");
printf("%d",num[i]);
printf("x");
}else{
printf(" - ");
printf("%d",-num[i]);
printf("x");
}
}
}else{
if(num[i]==1){
printf("x");
}else if(num[i]==-1){
printf("-x");
}else{
printf("%d",num[i]);
printf("x");
}
}
continue;
}
if(num[i]==1||num[i]==-1){
if(i!=8){//系数为1的基础项
if(countN==1){
if(num[i]>0){
printf("x^");
printf("%d",8-i);
}else{
printf("-x^");
printf("%d",8-i);
}
}else{
if(num[i]>0){
printf(" + ");
printf("x^");
printf("%d",8-i);
}else{
printf(" - ");
printf("x^");
printf("%d",8-i);
}
}
continue;
}else{//常数项
if(countN!=1){
if(num[i]>0){
printf(" + ");
printf("1");
}else{
printf(" - ");
printf("1");
}}else{
if(num[i]>0){
printf("1");
}else{
printf("-1");
}
}
break;
}
}
if(countN==1){
if(i==8){
printf("%d",num[i]);
}else{
printf("%d",num[i]);
printf("x^");
printf("%d",8-i);
}
}else{
if(i==8){
if(num[i]>0){
printf(" + ");
printf("%d",num[i]);
}else{
printf(" - ");
printf("%d",-num[i]);
}
}else{
if(num[i]>0){
printf(" + ");
printf("%d",num[i]);
printf("x^");
printf("%d",8-i);
}else{
printf(" - ");
printf("%d",-num[i]);
printf("x^");
printf("%d",8-i);
}
}
}
}
}
if(countZ==9){
printf("0");
}
printf("\n");
}
return 0;
}
不是太难,就是情况有点多,容易漏掉情况。例如最后一般性情况下也要注意常数项的输出,不能x^0。