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 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
思考
这里胡凡书上写的不全面,代码修正如下。
AC代码
#include <stdio.h>
#define max_n 1111
double p[max_n] = {};//这样也可以置初值为0
int main() {
int k, n, count = 0;
double a;
scanf("%d", &k);
count +=k;
for(int i = 0; i < k; i++) {
scanf("%d %lf", &n, &a);
p[n] += a;
}
scanf("%d", &k);
count +=k;
for(int i = 0; i < k; i++) {
scanf("%d %lf", &n, &a);
if(p[n]!=0) count--;//出现重合项-1
p[n] += a;
if(p[n]==0) count--;//出现重合项且一正一负抵消为0,再-1
}
// for(int i = 0; i < max_n; i++) {
// if(p[i] != 0) {
// count++;
// }//这样计数非零项是保险的
// }
printf("%d", count);
for(int i = max_n - 1; i >= 0; i--) {
if(p[i] != 0) printf(" %d %.1f", i, p[i]);
}
return 0;
}
A1002 A+B for Polynomials (25)(25 分)的更多相关文章
- PAT 甲级 1002 A+B for Polynomials (25 分)
1002 A+B for Polynomials (25 分) This time, you are supposed to find A+B where A and B are two polyno ...
- 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 pol ...
- pat 1002 A+B for Polynomials (25 分)
1002 A+B for Polynomials (25 分) This time, you are supposed to find A+B where A and B are two polyno ...
- 怎么设置BarTender中二维码大小为25*25
有小伙伴近期问了小编一个问题,说客户需要25*25大小的QR Code二维码,用BarTender怎么做出来?想要指定条形码的大小,还得BarTender符号与版本选项来帮忙.本文小编就来给大家详细讲 ...
- JAVA题目:正整数n若是其平方数的尾部,则称n为同构数 如:5*5=25, 25*25=625 问: 求1~99中的所有同构数
1 /*题目:正整数n若是其平方数的尾部,则称n为同构数 2 如:5*5=25, 25*25=625 3 问: 求1~99中的所有同构数 4 */ 5 //分析:将1-99分为1-9和10-99,用取 ...
- 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 A1009 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& ...
- 1002. A+B for Polynomials(25)—PAT 甲级
This time,you are supposed to find A+B where A+B are two polynomials. Input Each input file contains ...
随机推荐
- Soft skill
不要害怕让别人看到自己的无知 作为高级程序员的一个好处是,当别人问一些我不懂的问题时,我可以很淡然地告诉他们: 这个东西我也不懂,因为以前没有遇到过,不过我可以看一下,然后再告诉你. 当我还是一个初级 ...
- 用gethub下载ardupilot的最新源码
1进入gethub的官方网站https://github.com/作者:恒久力行 QQ:624668529 在搜索框内输入ardupilot并点击搜索点回车 2会看到很多工程,选择那 ...
- js技巧-使用reduce实现更简洁的数组对象去重和数组扁平化
Array.prototype.reduce()方法介绍: 感性认识reduce累加器: const arr = [1, 2, 3, 4]; const reducer = (accumulator, ...
- 零基础逆向工程32_Win32_06_通用控件_VM_NOTIFY
标准控件与可用控件 windows标准控件,标准控件总是可用的 Static Group Box Button Check Box Radio Button Edit ComboBox ListBox ...
- RING3到RING0
当我在说跳转时,说的什么? CPU有很多指令,不是所有的指令都能够随时用,比如 ltr指令就不是随便什么时候能用,在保护模式下,如果你不安规则来执行指令,CPU就会抛出异常,比如你在INTEL手册上就 ...
- Eucalyptus常用查询命令
前言: Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...
- Jquery里面的$(this)和this, 有什么区别
当你用的是jquery时,就用$(this),如果是JS,就用this $(this).html( $(this).html() + " BAM! "); 这个里的html()是J ...
- 观察者模式(Observe Pattern)
观察者模式: 当对象存在一对多关系时,使用观察者模式(Observe Pattern).例如:当一个对象被修改时,会通知它的依赖对象. 介绍: 1.意图:定义对象的一种一对多的依赖关系,当一个对象的状 ...
- firefox 提示 ssl_error_unsupported_version 的解决方法
访问一些HTTPS网站时尤其是国内网站 中文提示: 无法安全地连接 Firefox 无法保证您在 sx.ac.10086.cn 上的数据安全性,因为它使用 SSLv3,一个目前安全性欠佳的安全协议.专 ...
- 用TreeView控件遍历磁盘目录
实现效果: 知识运用: ListView控件中Items集合的Add方法 TteeView控件中Nodes集合的Add方法 实现代码: private void Form1_Load(object ...