A1002. A+B for Polynomials
This time, you are supposed to find A+B where A and B are two polynomials.
Input
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
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
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int K, n, count = ;
double poly1[] = {}, poly2[] = {}, a;
scanf("%d", &K);
for(int i = ; i < K; i++){
scanf("%d%lf", &n, &a);
poly1[n] = a;
}
scanf("%d", &K);
for(int i = ; i < K; i++){
scanf("%d%lf", &n, &a);
poly2[n] = a;
}
for(int i = ; i >= ; i--){
poly1[i] = poly1[i] + poly2[i];
if(poly1[i] != )
count++;
}
printf("%d", count);
for(int i = ; i >= ; i--){
if(poly1[i] != )
printf(" %d %.1lf", i, poly1[i]);
}
cin >> K;
return ; }
总结:
1、多项式加法,在项数不多的情况下直接开数组。
2、注意不用输出系数为0的项
A1002. A+B for Polynomials的更多相关文章
- A1002 A+B for Polynomials (25)(25 分)
1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...
- PAT甲级——A1002 A+B for Polynomials
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
仅有两个要注意的点: 如果系数为0,则不输出,所以输入结束以后要先遍历确定系数不为零的项的个数 题目最后一句,精确到小数点后一位,如果这里忽略了,会导致样例1,3,4,5都不能通过
- PAT A1002 A+B for Polynomials(25)
AC代码 转载自https://www.cnblogs.com/zjutJY/p/9413766.html #include <stdio.h> #include<string.h& ...
- PAT/简单模拟习题集(二)
B1018. 锤子剪刀布 (20) Discription: 大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负 ...
- PAT题目AC汇总(待补全)
题目AC汇总 甲级AC PAT A1001 A+B Format (20 分) PAT A1002 A+B for Polynomials(25) PAT A1005 Spell It Right ( ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- 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 ...
- 1002. A+B for Polynomials (25)
题目链接:https://www.patest.cn/contests/pat-a-practise/1002 原题如下: This time, you are supposed to find A+ ...
随机推荐
- :before添加图片,IE8兼容
这是项目开发中遇到的奇怪的小问题: 在IE8下出现按钮点击后消失了,鼠标点击页面后却又出现: 最初的代码:添加背景图片的方法,这样是存在兼容问题的. 更改后代码:content中添加图片,完美兼容IE ...
- flutter中使用svg
dependencies: flutter_svg: ^0.12.1 flutter packages get import 'package:flutter_svg/flutter_svg.dart ...
- Hbase表结构模型
- Java语言支持的3种变量类型
类变量(静态变量):独立于方法之外的变量,用 static 修饰. 实例变量(全局变量):独立于方法之外的变量,不过没有 static 修饰. 局部变量:类的方法中的变量. 例子如下: public ...
- java 中 的 字节流!
package cn.zhouzhou; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
- Nginx简单的负载均衡入门
nginx是用来管理tomcat的,只管理tomcat,并没有管理具体tomcat里面的项目,这里实现了简单的nginx管理两个tomcat的配置,注意upstream节点应该配置到service节点 ...
- cookie的域,路径
Cookie 的路径以及 Cookie 域 cookie 路径 cookie 一般都是由于用户访问页面而被创建的,可是并不是只有在创建 cookie 的页面才可以访问这个cookie.在默认情况下,出 ...
- Fourier Transform Complex Conjugate Discussion
FT of function $f(t)$ is to take integration of the product of $f(t)$ and $e^{-j\Omega t}$. By separ ...
- Tomcat的四种web应用部署方式详解
在Tomcat中有四种部署Web应用的方式,简要的概括分别是: (1)利用Tomcat自动部署 (2)利用控制台进行部署 (3)增加自定义的Web部署文件(%Tomcat_Home%\conf\Cat ...
- webpack——快速入门【一】
学习webpack https://github.com/webproblem/learning-article#webpack https://github.com/lengziyu/learn-w ...