PAT 1002
1002. A+B for Polynomials (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
注意对相加后系数为0的处理。
代码
1 #include <stdio.h>
2
3 int main()
4 {
5 int k1,k2;
6 double d_arr1[],d_arr2[],d_arr3[];
7 int i_arr1[],i_arr2[],i_arr3[];
8 int i,j,k;
9 while(scanf("%d",&k1) != EOF){
for(i=;i<k1;++i)
scanf("%d%lf",&i_arr1[i],&d_arr1[i]);
scanf("%d",&k2);
for(i=;i<k2;++i)
scanf("%d%lf",&i_arr2[i],&d_arr2[i]);
i = ;
j = ;
k = ;
while(i < k1 && j < k2){
if (i_arr1[i] == i_arr2[j]){
i_arr3[k] = i_arr1[i];
d_arr3[k] = d_arr1[i++] + d_arr2[j++];
if (d_arr3[k] != 0.0)
++k;
}
else if (i_arr1[i] > i_arr2[j]){
i_arr3[k] = i_arr1[i];
d_arr3[k++] = d_arr1[i++];
}
else{
i_arr3[k] = i_arr2[j];
d_arr3[k++] = d_arr2[j++];
}
}
for (;i<k1;++i){
i_arr3[k] = i_arr1[i];
d_arr3[k++] = d_arr1[i];
}
for (;j<k2;++j){
i_arr3[k] = i_arr2[j];
d_arr3[k++] = d_arr2[j];
}
printf("%d",k);
for(i=;i<k;++i){
printf(" %d %.1f",i_arr3[i],d_arr3[i]);
}
printf("\n");
}
return ;
}
PAT 1002的更多相关文章
- PAT 1002. A+B for Polynomials (25) 简单模拟
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
- PAT 1002 写出这个数 (20)(代码)
1002 写出这个数 (20)(20 分) 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10^100 ...
- 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 ...
- PAT 1002. 写出这个数 (20)
读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每 ...
- PAT 1002 Hello World for U (20)
Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. ...
- PAT 1002. A+B for Polynomials
思路:就是两个多项式做加法–指数相同的相加即可,输出的时候按照指数递减输出,并且系数为0的项不输出. AC代码 #include <stdio.h> #include <vector ...
- PAT 1002 写出这个数
https://pintia.cn/problem-sets/994805260223102976/problems/994805324509200384 读入一个自然数n,计算其各位数字之和,用汉语 ...
- PAT——1002. 写出这个数
读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每 ...
- PAT 1002. A+B for Polynomials (25)
This time, you are supposed to find A+B where A and B are two polynomials. Input Each input file con ...
随机推荐
- QT-【转】2D编程
Qt中提供了强大的2D绘图系统,可以使用相同的API在屏幕上和绘图·设备上进行绘制,主要基于QPainter.QPainterDevice和QPainterEngine这3个类. 1.QPainter ...
- Canvas入门(1):绘制矩形、圆、直线、曲线等基本图形
来源:http://www.ido321.com/968.html 一.Canvas的基础知识 Canvas是HTML 5中新增的元素,专门用于绘制图形.canvas元素就相当于一块“画布”,一块无色 ...
- 在Toast里面显示图片
关于怎么在Toast里面显示图片,首先自定义一个toast,在自定义一个布局,这个布局你想让toast显示什么样的布局就定义什么样的,然后在自定的布局中放一个ImageView,在把自己自定义 ...
- sizeof 字符数组
比较 #include <stdio.h> #include <string.h> int main(int argc, const char *argv[]) { char ...
- ecstore 后台登陆跳转到 api失败,中心请求网店API失败
解决过程没有具体参与,官方解决后回复的邮件,可以参考一下: 后台登陆错误图: 商派解决方法邮件: 特别注意:这个错误提示有时候也跟ecstore的nginx服务器伪静态有关,具体参考: htt ...
- 如何解决paramiko执行与否的问题
使用paramiko执行一些耗时比较长的命令的时候会出现实际上命令没有执行完就跳出的问题,怎么才能准确的判断命令执行完与否很重要,通过试验发现如下的方法可以解决这个难题: dabao_cmd = 'e ...
- CABasicAnimation精讲
前言 本教程写了这个效果图的demo,同时总结CABasicAnimation的使用方法. 看完gif动画完,看到了什么?平移.旋转.缩放.闪烁.路径动画. 实现平移动画 实现平移动画,我们可以通过t ...
- 转载JQuery 中empty, remove 和 detach的区别
转载 http://www.cnblogs.com/lisongy/p/4109420.html .empty() 描述: 从DOM中移除集合中匹配元素的所有子节点. 这个方法不接受任何参数. 这个 ...
- SGU 171 Sarov zones (贪心)
题目 SGU 171 相当好的贪心的题目!!!!! 题目意思就是说有K个赛区招收参赛队员,每个地区招收N[i]个,然后每个地区都有一个Q值,而N[i]的和就是N,表示总有N个参赛队员,每个队员都有 ...
- 一、Microsoft Dynamics CRM 4.0 SDK概述
Chapter 1. Microsoft Dynamics CRM 4.0 SDK Overview(SDK概述) You are probably reading this book because ...