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 ...
随机推荐
- log(A^B) = BlogA
令 x = logA, y = logB, z=log(AB) .2x = A, 2y = B, 2z = AB, 则有 2z = AB = (2x)^(2y) = 2x(2^y) ,有z = x*2 ...
- Cucumber 场景大纲 Scenario Outlines
引用链接:https://github.com/cucumber/cucumber/wiki/Scenario-Outlines script/cucumber --i18n zh-CN | feat ...
- iOS 本地缓存实现 方案借鉴
在手机应用程序开发中,为了减少与服务端的交互次数,加快用户的响应速度,一般都会在iOS设备中加一个缓存的机制,前面一篇文章介绍了iOS设备的内存缓存,这篇文章将设计一个本地缓存的机制. 功能需求 这个 ...
- python_1基础学习
2017年12月02日 20:14:48 独行侠的守望 阅读数:221 标签: python 更多个人分类: Python编辑版权声明:本文为博主原创文章,转载请注明文章链接. https://blo ...
- vue-cli之脚手架
一.创建VUE项目 npm install vue-cli -g vue init webpack myprject cd myproject npm run dev 补充: 组件:它是可扩展的htm ...
- LESS CSS非常实用实例应用
@charset "UTF-8"; @base-color:#333; // 圆角 .border-radius (@radius: 5px) { -webkit-border-r ...
- 编译64位geos库的经验总结
作者:朱金灿 来源:http://blog.csdn.net/clever101 使用CMake生成Win64的解决方案后,使用VS2010打开这个解决方案,然后 在"C/C++" ...
- JAVA继承与使用
说来惭愧,java学完已经两年了,开发也已经做了快一年了,现在才基本了解继承怎么用,平时都是在一个类中乱写一气.现在感觉原来学的知识真正运用起来还是具有一定的差距.希望能够先夯实基础,共勉.写一下自己 ...
- win10 U盘重装
之前用一键重装软件装系统后,D盘留下了一个PE系统,后来我装双系统装好Ubuntu后,打开Ubuntu结果出现了那个PE系统,最后没办法只好重装win10. 重装系统主要有三种方法,参见:链接 因为电 ...
- js 正则匹配(去掉html标签)
正则匹配去掉所有html标签 var a = "<span>999</span>" a = a.replace(/<[^>]+>/g,' ...