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 ...
随机推荐
- Unity C# 运用 GetSaveFileName() 导出Excel文件
本文原创,转载请注明出处:http://www.cnblogs.com/AdvancePikachu/p/6944870.html 唉哟,这次厉害咯,网上搜罗了好久,终于被我找到汉化的保存对话框了,根 ...
- 移植mavlink到stm32详细教程,后面附快速移植方法
一:准备材料: mavlink源码 stm32串口程序 1.mavlink源码: a.进入mavlink官网(http://qgroundcontrol.org/mavlink/s ...
- Vue日历
Vue生成日历,根据返回值将日期标红 HTML: <h1>CSS 日历</h1> <div id="calendar"> <div cla ...
- PHP函数:method_exists和function_exists
method_exists 检查类的方法是否存在 bool method_exists ( mixed $object , string $method_name ) 检查类的方法是否存在于指定的ob ...
- 使用tooltip显示jquery.validate.unobtrusive验证信息
通过重写CSS实现使用tooltip显示jquery.validate.unobtrusive验证信息,效果如图: 1. 在ViewModel中定义验证规则 [Display(Name = " ...
- OpenSSL命令---s_client
http://blog.csdn.net/as3luyuan123/article/details/16812071 用途: s_client为一个SSL/TLS客户端程序,与s_server对应,它 ...
- FW 数据库迁移之从oracle 到 MySQL
方式一: 手动方式导入导出 手动的方式导入, 就是操作步骤会比较繁琐一些. 对Table 的结构和数据: 1. 使用 SQL Developer 把 oracle 的 table 的schema 和 ...
- vue组件 $children,$refs,$parent的使用
如果项目很大,组件很多,怎么样才能准确的.快速的寻找到我们想要的组件了?? 1)$refs 首先你的给子组件做标记.demo :<firstchild ref="one"&g ...
- Java后台调用gcc编译C语言代码
想做一个能够在线编译代码运行的平台,Java和SQL已经支持了,因为是用Java写的后台,所以Java和SQL挺容易就实现了,做到支持C的时候就卡住了,网上搜了一下这种帖子好像很少. 我采取的办法是就 ...
- Python求列表中某个元素的下标
一.求列表中某个元素的下标 def findindex(org, x, pos=-1): counts = org.count(x) #先求出org中包含x的个数 if counts == 0: #个 ...