zoukankan      html  css  js  c++  java
  • PTA (Advanced Level) 1002 A+B for Polynomials

    1002 A+B for Polynomials

      This time, you are supposed to find A+B where A and B 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:

      K N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

      where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (,) are the exponents and coefficients, respectively. It is given that 1,0.

    Output Specification:

      For each test case you should output the sum of A and B 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 to 1 decimal place.

    Sample Input:

    2 1 2.4 0 3.2

    2 2 1.5 1 0.5

    Sample Output:

    3 2 1.5 1 2.9 0 3.2

    题目解析

       本题给出两个多项式,每个多项式按照 项数K 指数N1 系数aN1 指数N2 系数aN2 …… 指数NK 系数aNK 的方式给出,要求计算两个多项式的和。

           用一个double型ans来记录两个多项式的和 (下标为指数,下标对应的和为系数),每输入一组指数与系数,就将系数加入ans中对应项中去。在输入数据时应记录输入的最大指数与最小指数。

           遍历ans由最小指数到最大指数,获取系数不为0的项数记为cnt,cnt便是两个多项式的和的项数。

           出数cnt,由最大指数到最小指数遍历并输出每一个系数不为0的项即可。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int MAX= 1e5+10;
     4 double ans[MAX];
     5 int main()
     6 {
     7     int n;
     8     scanf("%d", &n);    //输入多项式A的项数
     9     int maxe = INT_MIN, mine = INT_MAX;
    10     //maxe保存最大指数    mine保存最小指数
    11     for(int i = 0; i < n; i++){ //输入多项式a
    12         int ea; //指数
    13         double temp;    //系数
    14         scanf("%d", &ea);   
    15         scanf("%lf", &temp);
    16         ans[ea] += temp;    //系数加入ans中对应项
    17         maxe = max(maxe, ea);   //记录最大指数
    18         mine = min(mine, ea);   //记录最小指数
    19     }
    20     scanf("%d", &n);    //输入多项式b
    21     for(int i = 0; i < n; i++){
    22         int eb; //指数
    23         double temp;    //系数
    24         scanf("%d", &eb);
    25         scanf("%lf", &temp);
    26         ans[eb] += temp;    //系数加入ans中对应项
    27         maxe = max(maxe, eb);   //记录最大指数
    28         mine = min(mine, eb);   //记录最小指数
    29     }
    30     int cnt = 0;
    31     //cnt记录A+B的项数
    32     for(int i = mine; i <= maxe; i++){  //由最小指数到最大指数遍历ans
    33         if(ans[i] != 0.0)   //若某项系数不为0则项数加1
    34             cnt++;
    35     }
    36     printf("%d", cnt);  //输出cnt
    37     for(int i = maxe; i >= mine; i--){  //由最大指数到最小指数遍历并输出每一个系数不为0的项
    38         if(ans[i] != 0.0)
    39             printf(" %d %.1f", i, ans[i]);
    40     }
    41     printf("
    ");
    42     return 0;
    43 }
  • 相关阅读:
    matplotlib种类
    matplotlib安装
    Python input保证输入为int类型
    centos7 安装 smplayer
    matplotlib与numpy
    Windows系统pip安装whl包
    python3打开winodows文件问题
    centos7 python3 pip
    Python实战171203统计
    Python实战171202元组访问
  • 原文地址:https://www.cnblogs.com/suvvm/p/10623350.html
Copyright © 2011-2022 走看看