zoukankan      html  css  js  c++  java
  • 1002. A+B for Polynomials

    1002. A+B for Polynomials (25)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    This time, you are supposed to find A+B where A and B are two polynomials.

    Input

    Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.

    Output

    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
     1 #include<stdio.h>
     2 #include<math.h>
     3 #include<stdlib.h>
     4 #include<string.h>
     5 
     6 int main()
     7 {
     8     double a[1010], b[1010];
     9     memset(a, 0, sizeof(a));
    10     memset(b, 0, sizeof(b));
    11     int ka, kb, i, j, x, y, maxa, maxb;
    12     scanf("%d", &ka);
    13     scanf("%d", &x);
    14     scanf("%lf", &a[x]);
    15     maxa = x;
    16     for(i = 1; i < ka; i++)
    17     {
    18         scanf("%d", &x);
    19         scanf("%lf", &a[x]);
    20     }
    21     scanf("%d", &kb);
    22     scanf("%d", &y);
    23     scanf("%lf", &b[y]);
    24     maxb = y;
    25     for(i = 1; i < kb; i++)
    26     {
    27         scanf("%d", &y);
    28         scanf("%lf", &b[y]);
    29     }
    30     if(maxa < maxb)
    31     {
    32         int temp = maxa;
    33         maxa = maxb;
    34         maxb = temp;
    35     }
    36     for(i = maxa; i >= 0; i--)
    37     {
    38         a[i] += b[i];
    39     }
    40     int count = 0;
    41     for(i = maxa; i >= 0; i--)
    42     {
    43         if(a[i] != 0)
    44             count++;
    45     }
    46     printf("%d", count);
    47     for(i = maxa; i >= 0; i--)
    48     {
    49         if(a[i] != 0)
    50         {
    51             printf(" %d %.1f", i, a[i]);
    52         }
    53     }
    54     printf("
    ");
    55     return 0;
    56 }
  • 相关阅读:
    分层图(了解一下,下一道比较好做)
    图论---The Captain
    数论
    NOIp复习计划
    20201116 Day4 卢卡斯定理
    20201113 Day3 斜率优化
    20201110Day2 分块
    20201030 day50 复习13:逆元、裴蜀定理
    20201030day50 模板全掌握
    20201029 day49 模拟(十八)
  • 原文地址:https://www.cnblogs.com/yomman/p/4268314.html
Copyright © 2011-2022 走看看