1002 A+B for Polynomials (25)(25 point(s))

problem

1002 A+B for Polynomials (25)(25 point(s))
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 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (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

anwser

#include<iostream>
#include<stdio.h>
#include<iomanip>
#include<cstring>

int main()
{
    freopen("test.txt", "r",stdin);
    float n[1001], n1[1001], n2[1001];
    memset(n, 0, 1001*sizeof(float));
    memset(n1, 0, 1001*sizeof(float));
    memset(n2, 0, 1001*sizeof(float));
    int a, b;

    std::cin>>a;
    for(int i = 0; i < a; i++){
        int temp1;
        float temp2;
        std::cin>>temp1>>temp2;
        n1[temp1] = temp2;
    }

    std::cin>>b;
    for(int i = 0; i < b; i++){
        int temp1;
        float temp2;
        std::cin>>temp1>>temp2;
        n2[temp1] = temp2;
    }

    int c = 0;
    for(int i = 0; i < 1001; i++){
        n[i] = n1[i] + n2[i];
//      std::cout<<n[i]<<i<<std::endl;
        if(n[i] != 0) c++;
    }
    std::cout<<c;
    for(int i = 1000; i >= 0; i--){
        if(n[i] != 0) {
            std::cout<<" "<<i<<" ";
            std::cout<<std::fixed<<std::setprecision(1)<<n[i];
//          printf("%.1f", n[i]);
        }
    }
    return 0;
}

/*
2 1 2.4 0 3.2
2 2 1.5 1 0.5
*/

experience

  • 注意头文件名
  • 注意边界条件以及输出格式

单词复习:

  • polynomials 多项式
  • exponents 范例,指数
  • coefficients 系数
  • respectively 分别的
  • accurate 精确的
  • decimal 小数

原文地址:https://www.cnblogs.com/yoyo-sincerely/p/9250986.html

时间: 2024-10-05 22:16:57

1002 A+B for Polynomials (25)(25 point(s))的相关文章

1009 Product of Polynomials (25)(25 分)

1009 Product of Polynomials (25)(25 分) 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 pol

PAT 1003 Emergency (25)(25 分)

1003 Emergency (25)(25 分) As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road betwe

A1082 Read Number in Chinese (25)(25 分)

A1082 Read Number in Chinese (25)(25 分) Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output "Fu" first if it is negative. For example, -123456789 is read as "Fu yi Yi er Qian san B

【PAT】1015 德才论 (25)(25 分)

1015 德才论 (25)(25 分) 宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人." 现给出一批考生的德才分数,请根据司马光的理论给出录取排名. 输入格式: 输入第1行给出3个正整数,分别为:N(<=10^5^),即考生总数:L(>=60),为录取最低分数线,即德分和才分均不低于L的考生才有资格被

PAT Advanced Level 1013 Battle Over Cities (25)(25 分)

1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any

1007 Maximum Subsequence Sum (25)(25 分)

1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequence is defined to be { N~i~, N~i+1~, ..., N~j~ } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence

1012 The Best Rank (25)(25 分)

1012 The Best Rank (25)(25 分) To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algebra), and E - English. At the mean ti

【PAT】1020 Tree Traversals (25)(25 分)

1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary

6.25(openstack环境部署)

安装有转发功能的DNSyum -y install bind bind-chrootvim /etc/named.confoptions {listen-on port 53 { 192.168.1.254; };//listen-on-v6 port 53 { ::1; };allow-query { any; }; recursion yes; forwarders { 172.40.1.10; }; dnssec-enable no; dnssec-validation no; 安装时间同