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里面的输出函数

最新文章

  1. Oracle客户端简易连接报错ORA-12154,TNS-03505
  2. Mac上编译C++报错
  3. SQL Server中截取字符串常用函数
  4. Linux 之 目录和文件
  5. TCP连接的三次握手和四次解散过程
  6. 防止SQL注入的,网站安全的一些常用解决方案
  7. windows安装django
  8. ASP.NET MVC视图中的@Html.xxx(...)
  9. Debian openvpn 配置
  10. javascript 操作复选框无效
  11. 移动前端javascript事件
  12. 安卓网络请求之——OkHttp学习
  13. pyfits 读取bintable
  14. HDU 3552 I can do it!
  15. JavaScript学习笔记(散)——addLoadEvent函数
  16. php数据库操作小要点
  17. MAC安装flutter开发环境
  18. DFA算法实现关键字查找(正则原理入门)
  19. 10.9-uC/OS-III任务管理
  20. 转发:tomcat的acess_log打印post请求参数,分析日志

热门文章

  1. 最新GHOST XP系统安全稳定版 V2016年
  2. 代码合并工具——Beyond Compare
  3. MonoDevelop几个常用的快捷键
  4. FATAL ERROR: Could not find ./bin/my_print_defaults的解决办法
  5. Oracle Latch的学习【原创】
  6. oracle和postgresql 递归查询父子关系记录语法区别
  7. android viewpager 图片翻页例子
  8. android WebView网页浏览器
  9. AngularJS基础概念
  10. Nova: 虚机的块设备总结 [Nova Instance Block Device]