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 ...
随机推荐
- 简单把webdriver的find_element方法写成函数
__author__ = 'jyd' from selenium.webdriver.common.by import By #driver webdriver实例化对象 #element 查询元素的 ...
- Jacoco远程统计代码覆盖率
Jacoco 什么是Jacoco? Jacoco是一个开源的Java代码覆盖率工具,Jacoco可以嵌入到Ant .Maven中,并提供了EclEmma Eclipse插件,也可以使用JavaAg ...
- js中的String数据类型
string中包含一些特殊的字符字面量,又叫转义序列,\n 意思是换行,\t 意为制表,\b意为空格,\r回车,\\斜杠. 1.ECMAScript中字符串是不可变的. 2.转换字符串的方法:toSt ...
- 【和我一起学python吧】Python解释执行原理
这里的解释执行是相对于编译执行而言的.我们都知道,使用C/C++之类的编译性语言编写的程序,是需要从源文件转换成计算机使用的机器语言,经过链接器链接之后形成了二进制的可执行文件.运行该程序的时候,就可 ...
- Chapter 2 创建一个应用
App Engine开发模式如下一般简单<1.The App Engine development model is as simple as it gets:>: 1.创建这个应用 2. ...
- 从一个开发的角度看负载均衡和LVS(转)
原文:http://blog.hesey.net/2013/02/introduce-to-load-balance-and-lvs-briefly.html 在大规模互联网应用中,负载均衡设备是必不 ...
- ocp 1Z0-042 121-178题解析
121. You want to create a new optimized database for your transactional production environment to be ...
- android EditText控制光标的位置
利用自定义键盘,需要手动删除编辑框中的文本时,会根据光标的位置来删除字符.那么,如何来控制光标呢,android为我们提供了哪些方法,来处理光标呢? 这里提供几个自己写的方法,根据这些方法可以满足在光 ...
- 工具栏停靠实现(toolbar docking)
// TODO: 如果不需要工具栏可停靠,则删除这三行 m_ToolBar_File.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_A ...
- window.parent != window 解决界面嵌套问题
页面在被嵌套的时,效果:,,如果用户点击“刷新”,该问题即可解决. 如果想通过代码解决的话,这个问题属于客户端的问题,不是服务器端的问题. 如果直接写:window.location.href = “ ...