PAT甲级 -- 1009 Product of 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 (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10, 0≤NK<⋯<N2<N1≤1000.
Output Specification:
For each test case you should output the product 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 up to 1 decimal place.
Sample Input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output:
3 3 3.6 2 6.0 1 1.6
25分:
注意:
1.注意指数最大会到2000, C数组应该开到2001
#include <iostream>
#include <cmath>
using namespace std;int ka, kb; //A和B中的非零项个数
double A[1001] = {0.0}, B[1001] = {0.0}, C[2001] = {0.0};
int expon;
double coe; //指数和系数
int mxa = -1, mxb = -1;int main()
{scanf("%d", &ka);for(int i = 0; i < ka; i++){scanf("%d %lf", &expon, &coe);A[expon] += coe;mxa = max(mxa, expon);}scanf("%d", &kb);for(int i = 0; i < kb; i++){scanf("%d %lf", &expon, &coe);B[expon] += coe;mxb = max(mxb, expon);}for(int i = 0; i <= mxa; i++){for(int j = 0; j <= mxb; j++){if(A[i] != 0 && B[j] != 0){C[i+j] += A[i] * B[j];}}}int ct = 0;for(int i = 2000; i >= 0; i--){if(C[i] != 0) ct++;}printf("%d", ct);for(int i = 2000; i >= 0; i--){if(C[i] != 0) printf(" %d %.1f", i, C[i]);}return 0;
}
2.柳神计算时输入多项式B时直接进行计算!参考:
#include <iostream>
using namespace std;
int main() {int n1, n2, a, cnt = 0;scanf("%d", &n1);double b, arr[1001] = {0.0}, ans[2001] = {0.0};for(int i = 0; i < n1; i++) {scanf("%d %lf", &a, &b);arr[a] = b;}scanf("%d", &n2);for(int i = 0; i < n2; i++) {scanf("%d %lf", &a, &b);for(int j = 0; j < 1001; j++)ans[j + a] += arr[j] * b;}for(int i = 2000; i >= 0; i--)if(ans[i] != 0.0) cnt++;printf("%d", cnt);for(int i = 2000; i >= 0; i--)if(ans[i] != 0.0)printf(" %d %.1f", i, ans[i]);return 0;
}
PAT甲级 -- 1009 Product of Polynomials (25 分)相关推荐
- PAT甲级1009 Product of Polynomials (25分)
PAT甲级1009 Product of Polynomials (25分) 题目: 题目分析:和之前有一道多项式相加很像,注意点是不仅仅数组系数会变,还可能会出现之前没有的指数,因此要开2001大小 ...
- PAT甲级1009 Product of Polynomials:[C++题解]多项式乘法、高精度乘法
文章目录 题目分析 题目链接 题目分析 多项用一个数组来表示,数组下标表示多项式的次幂,存的内容表示多项式的系数. 然后用两重循环来计算多项式的乘法: for i : 第二个式子for j:第一个式子 ...
- 【测试点0分析】1009 Product of Polynomials (25 分)
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 This time, you are supposed to find A×B where A and B are two pol ...
- 【PAT甲级 多项式相乘】1009 Product of Polynomials (25 分) C++ 全部AC
题目 思路 维护三个数组: arrA[1001]存储第一行数据 arrB[1001]存储第二行数据 c[1000000]存储计算结果 数组下标表示多项式的指数,数组存的内容表示多项式的系数 将arrA ...
- 【PAT】1009. Product of Polynomials (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1009 分析:简单题.相乘时指数相加,系数相乘即可,输出时按指数从高到低的顺序.注意点:多项式相 ...
- 1009 Product of Polynomials (25 分)【难度: 简单 / 知识点: 模拟】
https://pintia.cn/problem-sets/994805342720868352/problems/994805509540921344 注意:系数数据范围虽然是0-1000 但是乘 ...
- 17冬第二题 PAT甲级 1141 Ranking of Institutions (25分) 有点鸡贼
题目 After each PAT, the PAT Center will announce the ranking of institutions based on their students' ...
- PAT甲级-1021 Deepest Root(25分)
题目:1021 Deepest Root(25分) 分析:找出以某个节点为根时,最深的根,这题可能会超时要用vector来表示二维数组 疑问:代码一是第二次写的超时了,代码二是第一次写的AC了,找不出 ...
- PAT甲级--1007 Maximum Subsequence Sum (25 分)
题目详情 - 1007 Maximum Subsequence Sum (25 分) (pintia.cn) Given a sequence of K integers { N1, N2, .. ...
最新文章
- 详解Ubuntu10.10下Qt连接Mysql数据库
- AlarmManager与PendingIntent的联合使用(一)
- 第十章—DOM(0)—NODE类型
- 九度oj 题目1376:最近零子序列
- Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
- 高等数学下-赵立军-北京大学出版社-题解-练习10.5
- [C++ rudiment][转]typedef 使用
- centos mysql安装
- 医院计算机科学与技术笔试试题,医院信息科招收计算机科学与技术专业的笔试试题...
- redis操作之迭代器 hscan
- CAD手机端怎么将DWG格式的图纸文件转换成PDF格式?
- 【社区周会】2021-04-27 内容概要
- 一个安全删除文件的shell命令
- 中国企业数智化价值不高,主要是因为三个问题
- Matplotlib做动图(基础版)
- 分享Android单元测试
- 打印输入的字符串(C语言)
- 小米AI实验室入选《麻省理工科技评论》中国“2021人工智能创新研究院”
- php 数据库编码,如何在php和mysql数据库中正确编码字符
- c语言时钟程序整点报时,单片机开发的(带整点报时、音乐闹钟)电子时钟(一)...