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 ...
随机推荐
- SpringBoot | 第八章:统一异常、数据校验处理
前言 在web应用中,请求处理时,出现异常是非常常见的.所以当应用出现各类异常时,进行异常的捕获或者二次处理(比如sql异常正常是不能外抛)是非常必要的,比如在开发对外api服务时,约定了响应的参数格 ...
- $.ajax和$.load的区别
http://lib.csdn.net/article/jquery/35614?knId=646
- 从零开始的全栈工程师——js篇2.13(案例存放:三重数组渲染)
- Vue系列(1):单页面应用程序
前言:关于页面上的知识点,如有侵权,请看 这里 . 关键词:SPA.单个 HTML 文件.全靠 JS 操作.Virtual DOM.hash/history api 路由跳转.ajax 响应.按需加载 ...
- JAVA中日期格式转换各个字母代表含义
G Era 标志符 Text AD y 年 Year 1996; 96 M 年中的月份 Month July; Jul; 07 w 年中的周数 Number 27 W ...
- EBS应用重启
重启系统应用 cd $ADMIN_SCRIPTS_HOME ./adstpall.sh apps/apps ./adstrtal.sh apps/apps 在重启应用时,可能会出现并发管理器未启动的情 ...
- Android 超简单的拖动按钮 悬浮按钮 吸附按钮
第一种 第二种 第一种实现方法 xml布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/re ...
- ArcGIS中合并空间有压盖关系的要素属性
1.前言 在客户单位, 被客户问道这样一个问题“如何合并两个有压盖关系图层的属性信息?” 在工具箱里面可以使用以下工具解决: 2.处理过程 (1)在工具箱中选择Spatial Join工具,并设置相关 ...
- python_27_多级字典嵌套及操作
#key-value 字典无下标 所以乱序,key值尽量不要取中文 person_log={ '大二':{ 'Ya Nan':['free','cute','soso'], 'Sha sha':['微 ...
- python基础一 day16 匿名函数
def add(x,y): return x+y add = lambda x,y:x+yprint(add(1,2)) dic={'k1':10,'k2':100,'k3':30}def func( ...