Polynomial_0
@【注】两个一元多项式按照指数由大到小的顺序输入!
#include <stdio.h> #define MAXSIZE 50 struct PolyNode { int coefficient; int exponential; }; int main() { PolyNode a[MAXSIZE]; PolyNode b[MAXSIZE]; PolyNode c[2*MAXSIZE]; int n1, n2; scanf("%d", &n1); int i; for(i=0; i<n1; ++i) scanf("%d %d", &a[i].coefficient, &a[i].exponential); scanf("%d", &n2); int j; for(j=0; j<n2; ++j) scanf("%d %d", &b[j].coefficient, &b[j].exponential); int ind=0; for(i=0, j=0; (i<n1)&&(j<n2); ) { if(a[i].exponential>b[j].exponential) { c[ind].exponential=a[i].exponential; c[ind].coefficient=a[i].coefficient; ++i; } else if(a[i].exponential<b[j].exponential) { c[ind].exponential=b[j].exponential; c[ind].coefficient=b[j].coefficient; ++j; } else { c[ind].exponential=a[i].exponential; c[ind].coefficient=a[i].coefficient+b[j].coefficient; ++i; ++j; } ++ind; } while(i<n1) { c[ind].exponential=a[i].exponential; c[ind].coefficient=a[i].coefficient; ++i; ++ind; } while(j<n2) { c[ind].exponential=b[j].exponential; c[ind].coefficient=b[j].coefficient; ++j; ++ind; } printf("f(x)="); for(i=0; i<ind; ++i) { if(c[i].coefficient>1) { if(i!=0) printf("+"); printf("%dx^%d", c[i].coefficient, c[i].exponential); } else if(c[i].coefficient==1) { if(i!=0) printf("+"); printf("x^%d", c[i].exponential); } else if(c[i].coefficient==0) ; else if(c[i].coefficient==-1) printf("-x^%d", c[i].exponential); else printf("%dx^%d", c[i].coefficient, c[i].exponential); } printf("\n"); return 0; }