这 真 TM 是个奇怪的问题 PAT 1009 Product of polynomials 求救~~~~

求救在先!!!

思想

该题目很简单, 数据结构用 下标做指数, 值做系数来存储多项式, 就行了…

然而!!

我在最后统计系数非零的项数的时候, 出现了一个我无法理解的错误!
用visited[]作为判断该项数的系数是否为0, 如果为0 则visited[i]值为0, 否则为 1;
打印的时候, 判断 visited是否为1, 若为1 则打印…第三个测试点, 竟然错了???

但是

我直接判断 data[i] 是否为0, 测试点顺利通过…无语

原题如下


1009 Product of Polynomials (25 分)

This time, you are supposed to find A×BA\times BA×B where AAA and BBB are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

KKK N1N_1N1 aN1a_{N_1}aN1 N2N_2N2 aN2a_{N_2}aN2 ... NKN_KNK aNKa_{N_K}aNK

where KKK is the number of nonzero terms in the polynomial, NiN_iNi and aNia_{N_i}aNi (i=1,2,⋯,Ki=1, 2, \cdots , Ki=1,2,,K) are the exponents and coefficients, respectively. It is given that 1≤K≤101\le K \le 101K10, 0≤NK<⋯0NK<<N2<N11000.

Output Specification:

For each test case you should output the product of AAA and BBB in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 3 3.6 2 6.0 1 1.6
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB

代码如下:

#include 
#include 

int main(void){
  // 下标做指数, 值做系数
  double data1[3010], data2[3010];
  double data[3010];

  int k1, k2;
  int i, j;
  int e;
  double c;

  int es1[30], es2[30];  //存储 两行数的指数值
  int es[30], visited[3010];
  for(i = 0; i < 30; i++){
    es1[i] = -1;
    es2[i] = -1;
    es[i] = 0;
  }

  scanf("%d", &k1);
  for(i = 0; i < k1; i++){
    scanf("%d %lf", &e, &c);
    data1[e] = c;
    es1[i] = e;
  }

  scanf("%d", &k2);
  for(i = 0; i < k2; i++){
    scanf("%d %lf", &e, &c);
    data2[e] = c;
    es2[i] = e;
  }

  for(i = 0; i < 3010; i++){
    data[i] = 0;
    visited[i] = 0;

  }

  int t = 0;
  int count = 0;
  for(i = 0; i < k1; i++){
    for(j = 0; j < k2; j++){
        es[t] += es1[i] + es2[j];
        data[es[t]] += data1[es1[i]] * data2[es2[j]];
        //if(visited[es[t]]&&data[es[t]]!=0){
        //    count ++;
        visited[es[t]] = 1;
        //}
        if(data[es[t]] == (double)0){
          //count--;
          visited[es[t]] = 0;
        }
        t++;
    }
  }
  count=0;
  for(i=0;i<3010;i++){
    if(data[i] !=0)
      count++;
  }
  printf("%d", count);
  for(i = 3009; i >= 0; i--){
    if(data[i] != (double)0 ){   // j就是这里 !!!!!!!换成 visited, 第三个测试点过不去
        printf(" %d %.1lf", i, data[i] );
        
    }
  }



  return 0;
}

你可能感兴趣的:(PAT之路)