PAT A1002 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 (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 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
二、题意
单词 | 意思 |
---|---|
polynomial | 多项式 |
exponent | 指数 |
coefficient | 系数 |
respectively | 分别地,各自地 |
1 decimal place | 小数点第一位 |
给出两行,每行均为一个多项式。K为多项式中非零项的个数,Ni为多项式的指数,aNi为多项式的系数。求两个多项式的和,并以输入格式输出结果。非零项项数,以及高次到低次输出非零项的指数和系数
三、思路
定义一个数组,其中p[n]表示指数n的项的系数,初值为0。
四、注意
1、注意输出格式,从高次到低次
2、精确到一位小数
3、count计数非零项项数时,不要选择在读入时计数,存在正负相抵问题
五、代码
#include <cstdio>
using namespace std;
int main() {int m, n, e;float c;float a[1005] = { 0 };scanf_s("%d", &m);for (int i = 1; i <= m; i++) {scanf_s("%d %f", &e, &c);a[e] += c;}scanf_s("%d", &n);for (int i = 1; i <= n; i++) {scanf_s("%d %f", &e, &c);a[e] += c;}int count = 0;for (int i = 0; i <= 1000; i++) {if (a[i] != 0) count++;}printf("%d", count);for (int i = 1000; i >=0; i--) {//输出格式应该从高次到低次if (a[i] != 0.0)//判断非零项printf(" %d %.1f", i, a[i]);//%.1f精确到小数点后一位}return 0;
}
PAT A1002 A+B for Polynomials (25 分)相关推荐
- PAT甲级1009 Product of Polynomials (25分)
PAT甲级1009 Product of Polynomials (25分) 题目: 题目分析:和之前有一道多项式相加很像,注意点是不仅仅数组系数会变,还可能会出现之前没有的指数,因此要开2001大小 ...
- 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 ...
- PAT A1002 A+B for Polynomials(25)
AC代码 转载自https://www.cnblogs.com/zjutJY/p/9413766.html #include <stdio.h> #include<string.h& ...
- 【PAT】B1070 结绳(25 分)
此题太给其他25分的题丢人了,只值15分 注意要求最终结果最长,而且向下取整 #include<stdio.h> #include<algorithm> using names ...
- 【PAT甲】1051 Pop Sequence (25分)判断出栈顺序的合法性
problem 1051 Pop Sequence (25分) Given a stack which can keep M numbers at most. Push N numbers in th ...
- 【PAT乙】1065 单身狗 (25分)
1065 单身狗 (25分) "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤ ...
- 【PAT乙】1030 完美数列 (25分) 枚举
1030 完美数列 (25分) 给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列. 现在给定参数 p 和一些正整数,请你从中选择尽可能 ...
- 【PAT】1030 完美数列 (25 分)
1030 完美数列 (25 分) 给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列. 现在给定参数 p 和一些正整数,请你从中选择尽可 ...
- 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, .. ...
最新文章
- java vuser脚本_loadrunner12中JavaVuser脚本的编写
- 量子信息技术研究现状与未来
- Android服务之AIDL
- 产品管理必修课:发布新版本不等于改进产品
- Java(Android)线程池
- 深入研究 Mini ASP.NET Core,看看 ASP.NET Core 内部到底是如何运行的
- 我如何想成为Java
- 最大化BEA WebLogic Cluster的性能、可用性和安全
- python ctypes
- C语言的soap报文处理转义字符,webservice传递特殊字符时的解决办法
- 谁说互联网行业发展走到了尽头?
- Vue报错Module build failed Error Node Sass version 6.0.1 is incompatible with ^4.0.0.解决方案
- VirtualBox没有64位选项,无法安装64位的解决方法(zhuan)
- UVA12169 Disgruntled Judge
- LINUX Centos7利用七牛空间做定时备份 qshell定时备份,定时上传到七牛空间
- python 100 days github_GitHub - Andyhe2019/Python-100-Days: Python - 100天从新手到大师
- 前端实现鼠标拖拽功能
- 【SpringBoot实战】分布式定时任务锁Shedlock
- 转:著名的100个管理定律点评10
- 【活动】微软社区秋日巡讲——Windows Phone 应用开发训练营