pat 1002 A+B for polynomials (第二个测试点一直无法通过,最后解决)

题目链接:点击


 

题目理解:

1.K N1 aN1 N2 aN2 ... NK aNK,  k代表的是多项式的项数, NK是多项式的指数, aNK  是多项式的的系数

其中1<=K<=10, k∈N+,  0 <= NK < ... < N2 < N1 <=1000 (注意这里的递减关系)

2.两个多项式相加,要按照以下的格式输入输出

3.输出的系数部分保留一位小数

例:

输入:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

输出:

3 2 1.5 1 2.9 0 3.2

 

注意点:

1.输出格式

2.不要输出相加系数为零的项,完全抵消的话,输出0;

3.注意数组越界问题(我因为数组范围太小, 第二个测试点一直无法通过,但是报的是答案错误而不是段错误,导致我看了好久的程序,

后来输入允许的最多项后, vs2008 报出stack around the variable “” was corrupted的错误。具体的问题详解请百度,扩大数组范围后,问题解决)

 


源代码如下(附两种方法,注释部分方法代码来源于https://www.cnblogs.com/brucekun/p/6511922.html):

 

  1 #include
  2 void getData(double *arr) 
  3 {
  4     double count=0;
  5     int i=1;
  6     scanf("%lf", &count);
  7     
  8     arr[0] = count; 
  9     count = count*2;
 10     while(count>0)
 11     {
 12         scanf(" %lf", &arr[i]);
 13         i++;
 14         count--;
 15     }
 16     return;
 17 }
 18 int main()
 19 {
 20     double a[21] = {0};
 21     double b[21] = {0};
 22     double c[42] = {0}; 
 23     int i=1, k=1;
 24     int tmp2;
 25     char d;
 26     getData(a);
 27     scanf("%c", &d); //用来吸收多余的换行
 28     getData(b);
 29     tmp2 = 1;
 30     for(i = 1, k = 1; i<=a[0]*2 && k<=b[0]*2;) {
 31         if (a[i]<b[k]) {
 32             c[tmp2] = b[k];
 33             c[tmp2+1] = b[k+1];
 34             k = k+2;
 35             tmp2 = tmp2+2;
 36         } else if (a[i]>b[k]) {
 37             c[tmp2] = a[i];
 38             c[tmp2+1] = a[i+1];
 39             tmp2 = tmp2+2;
 40             i = i+2;
 41         } else {
 42             if((a[i+1]+b[k+1])!=0) {
 43                 c[tmp2] = a[i];
 44                 c[tmp2+1] = a[i+1]+b[k+1];
 45                 tmp2 = tmp2+2;
 46             }
 47             
 48             i = i+2;
 49             k = k+2;
 50         }
 51     }
 52     while(i<=a[0]*2) {
 53         c[tmp2] = a[i];
 54         c[tmp2+1] = a[i+1];
 55         tmp2 = tmp2+2;
 56         i = i+2;
 57     }
 58 
 59     while(k<=b[0]*2) {
 60         c[tmp2] = b[k];
 61         c[tmp2+1] = b[k+1];
 62         k = k+2;
 63         tmp2 = tmp2+2;
 64     }
 65     tmp2 = tmp2-1; //减去最后多加的一个
 66     c[0] = tmp2/2;
 67     printf("%.0lf", c[0]);
 68     for(i=1; i<=tmp2; i++) {
 69         if((i%2) != 0) {
 70             printf(" %.0lf", c[i]);
 71         } else {
 72             printf(" %.1lf", c[i]);
 73         }
 74     }
 75     return 0;
 76 }
 77 
 78 /*
 79 #include
 80 
 81 struct node
 82 {
 83     int exp;
 84     double coef;
 85 }a[11], b[11];
 86 int main()
 87 {
 88     double c[1001]={0};
 89     int sum = 0;
 90     int m = 0;
 91     int n = 0;
 92     int i = 0;
 93     int k = 0;
 94     scanf("%d ", &m);
 95     sum = m;
 96     for(i=0; i 97         scanf("%d %lf", &a[i].exp, &a[i].coef);
 98         c[a[i].exp] = a[i].coef;
 99     }
100 
101     scanf(" %d ", &n);
102     sum += n;
103     for(i=0; i104         scanf("%d %lf", &b[i].exp, &b[i].coef);
105         c[b[i].exp] += b[i].coef;
106     }
107     
108     for(i=0; i109         for(int k=0; k110             if(a[i].exp==b[k].exp) {
111                 sum--;
112                 if((a[i].coef+b[k].coef)==0)
113                     sum --;
114             }
115         }
116     }
117 
118     printf("%d", sum);
119     for(int i=1000; i>=0; i--) {
120         if(c[i] != 0) {
121             printf(" %d %.1lf",i, c[i]);
122         }
123     }
124     return 0;
125 }*/

 



转载于:https://www.cnblogs.com/tpwBlog/p/8992472.html

你可能感兴趣的:(pat 1002 A+B for polynomials (第二个测试点一直无法通过,最后解决))