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. IIS 部署 web service

    1.在控制台检查 IIS 功能是否已经全部启用 2.重新注册IIS 3.设定程序池的正确版本

  2. python __双划线 参数

    ''' >>> Class1.__doc__ # 类型帮助信息 'Class1 Doc.' >>> Class1.__name__ # 类型名称 'Class1' ...

  3. Python - 关于属性访问的优先级,属性访问顺序,python attributel lookup,类和实例访问属性的顺序

    object.__getattribute__(self, name) 类中的数据描述符 object.__dict__.get(name) 自身属性字典 object.__class__.__dic ...

  4. 世界协调时间(UTC)与中国标准时间

    整个地球分为二十四时区,每个时区都有自己的本地时间.在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated).UTC与格 ...

  5. CSP-201609-3 炉石传说

    问题描述 <炉石传说:魔兽英雄传>(Hearthstone: Heroes of Warcraft,简称炉石传说)是暴雪娱乐开发的一款集换式卡牌游戏(如下图所示).游戏在一个战斗棋盘上进行 ...

  6. idea 启动java项目报 java: 程序包org.apache.jasper.tagplugins.jstl.core不存在

    File -- Project Structure

  7. 线程池ExecutorService的使用及其正确关闭方法

    创建一个容量为5的线程池 ExecutorService executorService = Executors.newFixedThreadPool(5); 向线程池提交15个任务,其实就是通过线程 ...

  8. Java中获取MongoDB连接的方法

    首先是所需jar包,Maven中的配置如下: <dependency> <groupId>org.mongodb</groupId> <artifactId& ...

  9. Fiddler一次性发多个请求

    Fiddler一次发送多个请求 选中某个请求: 选中 : Raw, 将request数据拷出: 包含请求header和request body 替换request header里面的ASP.NET_S ...

  10. Python语言——列表生成式

    生成[1x1, 2x2, 3x3, ..., 10x10]列表: >>> L = [] >>> for x in range(1, 11):... >> ...