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 ...
随机推荐
- 从头学pytorch(四) softmax回归实现
FashionMNIST数据集共70000个样本,60000个train,10000个test.共计10种类别. 通过如下方式下载. mnist_train = torchvision.dataset ...
- uniGUI之UniPopupMenu和右键菜单(27)
0]MainModule的BrowserOptions.boDisableMouseRightClick设置为Trure; 1]控件的OnCellContextClick的事件 procedure T ...
- 【Go语言系列】第三方框架和库——GIN:快速入门
要求要安装Gin软件包,需要:1.安装Go(需要1.11+版本)2.设置Go工作区 安装1.下载并安装 gin: $ go get -u github.com/gin-gonic/gin 2.将 gi ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 网格系统实例:堆叠的水平
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Ubuntu 12.10 安装vim出错
在Ubuntu 12.10中安装vim时出现了如下提示: 正在读取软件包列表... 完成正在分析软件包的依赖关系树 正在读取状态信息... 完成 有一些软件包无法被安装.如果您用的是 unstable ...
- java中使用静态字段和构造函数跟踪某个类所创建对象的个数
对于这个问题,我们都知道java中使用类时会自动调用构造函数.按照这个思路我们可以定义一个static int 形的常量count 然后将count++放入这个类的构造函数中,这样只要输出count的 ...
- leetcode 0211
目录 ✅ 1217. 玩筹码 描述 解答 c java py ✅ 206. 反转链表 描述 解答 c java py ✅ 922. 按奇偶排序数组 II 描述 解答 c 双指针soldier tddo ...
- [ DLPytorch ] 注意力机制&机器翻译
MachineTranslation 实现过程 rstrip():删除 string 字符串末尾的指定字符(默认为空格). 语法:str.rstrip([chars]) 参数:chars -- 指定删 ...
- jmeter 并发控制
1.吞吐控制器以线程组的请求sampler为控制对象, 2.事务控制器: 3.同步定时器syn timer:对某线程组下任意的sampler任意位置作用为,有序控制单个sampler的并发先sampl ...
- Linux中制作静态库
静态库生成: 1.第一步:生成.o文件 2.第二步:将所有.o文件打包 ar src libMyTest.a *.o 生成出libMyTest.a 3.使用: 第一种:gcc main.c ...