1002. A+B for Polynomials
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
#include <iostream>
#include <cstdio>
#include <vector> using namespace std;
class node{
public:
int expon;//exponents
double coe;//coefficients
node(int _expon, double _coe) :expon(_expon), coe(_coe){}
~node(){}
}; int main(void)
{
int N1;
cin >> N1;
vector<node> List1;
for (size_t i = ; i < N1; i++)
{
int expon; double coe;
cin >> expon >> coe;
List1.push_back(node(expon, coe));
}
int N2;
cin >> N2;
vector<node> List2;
for (size_t i = ; i < N2; i++)
{
int expon; double coe;
cin >> expon >> coe;
List2.push_back(node(expon, coe));
} vector<node>::iterator it1, it2;
it1 = List1.begin(); it2 = List2.begin(); vector<node> List3;
while( (it1 != List1.end()) && (it2 !=List2.end()) )
{ if ((*it1).expon == (*it2).expon)
{ double coe_sum = (*it1).coe + (*it2).coe;
if (coe_sum != )
List3.push_back(node((*it1).expon, coe_sum));
it1++; it2++;
}
else if ((*it1).expon > (*it2).expon)
{
List3.push_back(node((*it1).expon, (*it1).coe));
it1++;
}
else if ((*it1).expon < (*it2).expon)
{
List3.push_back(node((*it2).expon, (*it2).coe));
it2++; } }
while (it1 != List1.end()){ List3.push_back(node((*it1).expon, (*it1).coe)); it1++; }
while (it2 != List2.end()){ List3.push_back(node((*it2).expon, (*it2).coe)); it2++; } cout << List3.size();
for (size_t i = ; i < List3.size(); i++)
{
cout << " " << List3[i].expon;
printf(" %0.1f", List3[i].coe);
}
cout << endl;
return ;
}
分析: 只需要注意一下输出的格式即好,这里用了C里面的输出函数
1002. A+B for Polynomials的更多相关文章
- 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 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 ...
- PTA (Advanced Level) 1002 A+B for Polynomials
1002 A+B for Polynomials This time, you are supposed to find A+B where A and B are two polynomials. ...
- PAT甲 1002. A+B for Polynomials (25) 2016-09-09 22:50 64人阅读 评论(0) 收藏
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
- 1002 A+B for Polynomials (25)(25 point(s))
problem 1002 A+B for Polynomials (25)(25 point(s)) This time, you are supposed to find A+B where A a ...
- 【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 polynom ...
- PAT甲级 1002 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 po ...
- 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 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 ...
随机推荐
- xcode中info.plist文件相关问题
<一>关于提示http://访问网络不安全的解决方法 提示错误: App Transport Security has blocked a cleartext HTTP (http://) ...
- 网络编程---HTTP
URL: 什么是URL ? URL的全称是Uniform Resource Locator(统一资源定位符) 通过1个URL,能找到互联网上唯一的1个资源 URL就是资源的地址.位置,互联网上的每个资 ...
- MyEclipse、Eclipse优化设置
第一步: 取消自动validation validation有一堆,什么xml.jsp.jsf.js等等,我们没有必要全部都去自动校验一下,只是需要的时候才会手工校验一下! 取消方法: windows ...
- Android Studio中怎么使用DDMS工具?
随着android studio的广泛使用,开发人员对相关工具的使用需求更加凸显.昨天在一个android studio教程网站上,看到一篇有关DDMS工具使用的相关知识,感觉很不错,分享给大家,一起 ...
- 优化SQLServer——表和分区索引
概念: 简单地说,分区是将大型的对象(如表)分成更小的且易于管理的小块.分区的基本单位是行,需要注意的是与分区视图不同的地方时,分区必须位于同一个数据库内. 分区的原因: 对于非 ...
- java容器(java编程思想第四版-读书笔记)
容器类库图 List(interface) 次序是List最重要的特点:它保证维护元素特定的顺序.List为Collection添加了许多方法,使得能够向List中间插入与移除元素.(这只推荐L ...
- 烂泥:nagios学习(四):pnp4nagios图形化绘制nagios数据
本文由秀依林枫提供友情赞助,首发于烂泥行天下 在nagios安装完毕后,我们也添加了一些监控对象,但是你会发现nagios只是简单的给我们列出那些监控对象是正常的,而没有把这些监控对象的数据进行整合. ...
- 烂泥:源码安装apache
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近要开始学习nagios监控方面的知识了,但是nagios与apache结合的比较紧密,所以本篇文章就先把apache的源码安装学习下. 我们现在分以 ...
- 烂泥:【解决】NFS服务器使用showmount –e命令报错
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 今天在NFS服务器使用showmount –e查看NFS共享目录时,发现系统一直显示如下错误: clnt_create: RPC: Port mappe ...
- CentOS下mysql默认安装位置
如果采用RPM包安装,安装路径应在/usr/share/mysql目录下 mysqldump文件位置:/usr/bin/mysqldump mysqli配置文件: /etc/my.cnf或/usr/s ...