1002 A+B for Polynomials (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 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

  0是零多项式,在题中将之归于“0项”多项式。当然了,实际是没有0项多项式的,只有零多项式,但是非要输出个结果,0还是合理的

我用了另一种思路,用数组模仿链表的实现。将A、B两多项式由次数从高到低依次计算,存入新数组。开个1000 的数组实在是浪费空间

  疑问:

    最初最后一个测试点没过的时候,我以为又是小负数近似格式的问题,便将系数绝对值小于0.05的项全忽略了。

#include <math.h>
#include <stdio.h>
#include <stdlib.h> typedef struct Poly
{
double coef;
int exp;
} Poly[]; int main(void)
{
int Ka, Kb, Ksum = ;
Poly A, B, Sum; scanf("%d", &Ka);
for (int i = ; i < Ka; ++i)
{
scanf("%d %lf", &A[i].exp, &A[i].coef);
} scanf("%d", &Kb);
for (int i = ; i < Kb; ++i)
{
scanf("%d %lf", &B[i].exp, &B[i].coef);
} int i = , j = ;
while (i < Ka || j < Kb)
{ //类似于链表的多项式相加
if (i == Ka || (j < Kb && A[i].exp < B[j].exp))
{ //多项式B长 或者 多项式B指数在A中不存在
Sum[Ksum].exp = B[j].exp;
Sum[Ksum].coef = B[j++].coef;
}
else if (j == Kb || (i < Ka && A[i].exp > B[j].exp))
{ //多项式A长 或者 多项式A指数在B中不存在
Sum[Ksum].exp = A[i].exp;
Sum[Ksum].coef = A[i++].coef;
}
else
{ //
Sum[Ksum].exp = A[i].exp;
Sum[Ksum].coef = A[i++].coef + B[j++].coef;
}
if (fabs(Sum[Ksum].coef) >= 0.05)
{ //小于这个值的被舍去了
Ksum++;
}
} printf("%d", Ksum); for (int i = ; i < Ksum; i++)
{
printf(" %d %.1lf", Sum[i].exp, Sum[i].coef);
} return ;
}

C++版本:

  用了Map和vector,MAP, 将整型的exp最为Map的关键字,再用二维的vector存储结果

#include <iostream>
#include <map>
#include <vector> using namespace std; int main(void)
{
map<int, float> poly1;
int K, exp;
float cof; scanf("%d", &K);
for (int i = ; i < K; ++i)
{ scanf("%d%f", &exp, &cof);
poly1[exp] += cof;
} scanf("%d", &K);
for (int i = ; i < K; ++i)
{ scanf("%d%f", &exp, &cof);
poly1[exp] += cof;
} vector<pair<int, float>> res;
for (map<int, float>::reverse_iterator it = poly1.rbegin(); it != poly1.rend(); it++)
{
if (it->second != ) // 两个系数和为0的得过滤掉
{
res.push_back(make_pair(it->first, it->second));
}
} printf("%lu", res.size());
for (int i = ; i < res.size(); ++i)
{
printf(" %d %.1f", res[i].first, res[i].second);
}
return ;
}

PAT不易,诸君共勉!

P1002 A+B for Polynomials (25分)的更多相关文章

  1. 1009 Product of Polynomials (25分) 多项式乘法

    1009 Product of Polynomials (25分)   This time, you are supposed to find A×B where A and B are two po ...

  2. PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值

    题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...

  3. PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...

  4. PAT Advanced 1009 Product of Polynomials (25 分)(vector删除元素用的是erase)

    This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...

  5. PAT Advanced 1002 A+B for Polynomials (25 分)(隐藏条件,多项式的系数不能为0)

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

  6. PAT 1002 A+B for Polynomials (25分)

    题目 This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: E ...

  7. 1002 A+B for Polynomials (25分)

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

  8. 【PAT甲级】1002 A+B for Polynomials (25 分)

    题意:给出两个多项式,计算两个多项式的和,并以指数从大到小输出多项式的指数个数,指数和系数. AAAAAccepted code: #include<bits/stdc++.h> usin ...

  9. 【PAT甲级】1009 Product of Polynomials (25 分)

    题意: 给出两个多项式,计算两个多项式的积,并以指数从大到小输出多项式的指数个数,指数和系数. trick: 这道题数据未知,导致测试的时候发现不了问题所在. 用set统计非零项时,通过set.siz ...

随机推荐

  1. 创业学习---《如何预判创业可行性》--B-1.预判模块---HHR计划---以太一堂

    <如何预判创业可行性>----对创业进行占卜 一,<开始学习> 1,预热思考题: (1)预判一个模式的可行性.你有一个朋友要创业,给你讲了他的创业计划,你帮他判断一下是否靠谱. ...

  2. np.multiply

    用法:np.multiply(x1,x2),作用:逐元素相乘,若x1和x2均为标量,则返回标量 x1=np.array([,,]) x2=np.array([,,]) np.multiply(x1,x ...

  3. twisted task.cpperator

    twisted task.cpperator 1.      twisted task.cpperator 1.1.    简介-cooperator 官方文档: https://twistedmat ...

  4. JavaScript 种一颗二叉树

    /* 实现一颗树 结点类:Tree 包含左子树left,右子树right,根节点root,缺省为null 构造设置value 树类:Trees 构造:默认根节点为null insert: 如果当前根节 ...

  5. POJ3268 Silver Cow Party (建反图跑两遍Dij)

    One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co ...

  6. 高级T-SQL进阶系列 (一)【中篇】:使用 CROSS JOIN 介绍高级T-SQL

    [译注:此文为翻译,由于本人水平所限,疏漏在所难免,欢迎探讨指正] 原文连接:传送门. 当一个CROSS JOIN 表现得如同一个INNER JOIN 在上一章节我提到当你使用一个CROSS JOIN ...

  7. Linux 上安装 Mysql 设置root密码问题

    Ubuntu 18.10.1 Mysql 5.7.26-0 安装mysql apt-get install mysql-server 安装完可以直接使用,但是新版本在安装过程中没有提示设置root用户 ...

  8. GIS中DTM/DEM/DSM/DOM的含义

    DTM(Digital Terrain Model):数字地面模型,是一个表示地面特征空间分布的数据库,一般用一系列地面点坐 标(x,y,z)及地表属性(目标类别.特征等)绗成数据阵列,以此组成数字地 ...

  9. python批量提取哔哩哔哩bilibili视频

    # -*- coding: utf-8 -*- """ Created on Tue Jan 29 13:26:41 2019 @author: kwy "&q ...

  10. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:内联子标题

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...