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 ...
随机推荐
- web安全测试&渗透测试之sql注入~~
渗透测试概念: 详见百度百科 http://baike.baidu.com/link?url=T3avJhH3_MunEIk9fPzEX5hcSv2IqQlhAfokBzAG4M1CztQrSbwsR ...
- <转>ERP的测试用例模板
1页面部分(1) 页面清单是否完整(是否已经将所需要的页面全部都列出来了)(2) 页面是否显示(在不同分辨率下页面是否存在,在不同浏览器版本中页面是是否显示)(3) 页面在窗口中的显示是否正确.美观( ...
- ubuntu安装nginx+php
1.安装nginx aptitude search nginx sudo apt-get install nginx 2.安装php sudo apt-get install php5 sudo ap ...
- PHP中我经常容易混淆的三组函数
原文:http://www.ido321.com/1252.html 一.htmlentities() 和htmlspecialchars() 1.htmlentities() 1.1 功能:把字符 ...
- Android Environment 类详解
Android应用开发中,常使用Environment类去获取外部存储目录,在访问外部存储之前一定要先判断外部存储是否已经是可使用(已挂载&可使用)状态, 并且需要在AndroidManife ...
- <Araxis Merge>Windows平台下的Merge概览
它是什么 Merge是一个来自Araxis的可视化文件比较/合并及文件夹同步的应用程序. 用户界面使用英语.德语.日语.法语.国际西班牙语.汉语(繁体和简体)进行本地化了. 优势 对于软件工程师和网站 ...
- hadoop2.7【单节点】单机、伪分布、分布式安装指导
问题导读 1.从本文部署实际部署,总结本地模式.伪分布.分布式的区别是什么?2.单机是否是伪分布?3.本地模式是否可以运行mapreduce? 来源:about云 http://www.aboutyu ...
- Nginx的代理和反向代理
什么是代理? 代理是为网络用户代理了来访问网络的,比如Google agent代理FQ. 什么是反向代理? 以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服 ...
- 利用phantomjs模拟QQ自动登录
之前为了抓取兴趣部落里的数据,研究了下QQ自动登录. 当时搜索了一番,发现大部分方法都已经失效了,于是准备自己开搞. 第一个想到的就是参考网上已有方案的做法,梳理登陆js的实现,通过其他语言重写.考虑 ...
- jQuery基础学习5——JavaScript方法获取页面中的元素
给网页中的所有<p>元素添加onclick事件 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...