P1002 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 N1 aN1 N2 aN2 ... NK aNK
where K is the number of nonzero terms in the polynomial, Ni and aNi (,) 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 的数组实在是浪费空间
疑问:
。
#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分)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【PAT甲级】1002 A+B for Polynomials (25 分)
题意:给出两个多项式,计算两个多项式的和,并以指数从大到小输出多项式的指数个数,指数和系数. AAAAAccepted code: #include<bits/stdc++.h> usin ...
- 【PAT甲级】1009 Product of Polynomials (25 分)
题意: 给出两个多项式,计算两个多项式的积,并以指数从大到小输出多项式的指数个数,指数和系数. trick: 这道题数据未知,导致测试的时候发现不了问题所在. 用set统计非零项时,通过set.siz ...
随机推荐
- https://www.cnblogs.com/lfs2640666960/p/8529115.html
https://www.cnblogs.com/lfs2640666960/p/8529115.html
- jquery--获取多选框的值、获取下拉多选框的值
获取多选框的值 var packageCodeList=new Array(); $('#server_id:checked').each(function(){ packageCodeList.pu ...
- web渗透(转)
某天比较无聊,听一个朋友推荐httpscan这款工具,于是就下载下来试试. 首先对某学校网段开始进行测试. 1 python httpscan.py **.**.**.0/24 测试时发现有个比较 ...
- Maven中配置jdk的版本
在单个项目中配置 在maven项目的pom.xml文件中加入以下内容 <build> <plugins> <plugin> <groupId>org.a ...
- 人工智能、大数据、物联网、区块链,四大新科技PK,你更看好谁?
最近行业中备受关注并且非常火热的产业有哪些呢?小编这边总结了一下,一共有4个,分别是人工智能.大数据.物联网和区块链,这四种新科技也一直是蓄势待发,未来将引领新一代的科技成长,也会带给人类很多更方便快 ...
- Vue 实现全局使用sass, less变量
首先 运行这行命令 npm install sass-resources-loader --save-dev: 在项目的build/utils.js中,找到 function generateLo ...
- #P4770 [NOI2018]你的名字 的题解
题目背景 实力强大的小A 被选为了ION2018 的出题人,现在他需要解决题目的命名问题. 题目描述 小A 被选为了ION2018 的出题人,他精心准备了一道质量十分高的题目,且已经把除了题目命名以外 ...
- i.MX RT600之DSP开发环境调试篇
i.MX RT600的Cadence Xtensa HiFi 4 Audio DSP 是一个高度优化过的音频处理器,主频高达600MHz,专门为音频信号的编码.解码以及预处理和后处理模块而设计,功能十 ...
- WEB安全 - XSS,CSRF
1. CSRF参考 https://www.ibm.com/developerworks/cn/web/1102_niugang_csrf/ https://en.wikipedia.org/wiki ...
- hbase单机版安装
hbase单机版安装 1. hbase单机版安装 HBase的安装也有三种模式:单机模式.伪分布模式和完全分布式模式. hbase依赖于Hadoop和Zookeeper. 这里安装的是单机版 ...