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 ...
随机推荐
- IOS学习资源收集--开发UI控件相关
收集的一些本人了解过的iOS开发UI控件相关的代码资源(本文持续补充更新) 内容大纲: 1.本人在github上也上传了我分装好的一些可重复利用的UI控件 2.计时相关的自定义UILabel控件 正文 ...
- 微信企业号api调用频率
主动调用的频率限制 当你获取到AccessToken时,你的应用就可以成功调用企业号后台所提供的各种接口以管理或访问企业号后台的资源或给企业号成员发消息. 为了防止企业应用的程序错误而引发企业号服务器 ...
- yii2发送邮件教程
作者:白狼 出处:http://www.manks.top/article/yii2_swiftMailer本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接, ...
- 最受欢迎的Java第三方库
前言 翻译自programcreek: 典型的Java项目通常会依赖一些第三方库,本文总结了一些最受欢迎的Java库,这些类库在各种应用程序中被广泛使用: 当然,Java SDK是最广泛使用的Java ...
- 10个关于Java异常的常见问题
这篇文章总结了十个经常被问到的JAVA异常问题: 1.检查型异常VS非检查型异常 简单的说,检查型异常是指需要在方法中自己捕获异常处理或者声明抛出异常由调用者去捕获处理: 非检查型异常指那些不能解决的 ...
- List对象去重
public class User { public int Id { get; set; } public string Name { get; set; } } public class User ...
- Let's Encrypt 正式出發(免费HTTPS证书即将到来)
转自:https://blog.gslin.org/archives/2015/10/20/6073/lets-encrypt-%E6%AD%A3%E5%BC%8F%E5%87%BA%E7%99%BC ...
- IE下innerText与FoxFire下textContent属性的不同
<div> 我是中国 人 我<br/>爱 自己 的<div>祖国</div>. <div> IE下输出 我是中国 人 我 爱 自己 的 祖国 ...
- python关键字,运算符
关键字: and且 or 或 not否 del import导入 from import的来源 whilewhile循环 for for循环 if elif else条件结构 break contin ...
- android viewpager 图片翻页例子
使用ViewPager这个类可以轻松实现多个页面的滑动功能 viewpager默认在工具界面上是找不到的,需求添加android-support-v4.jar包: 如果没有找到,需要打开Android ...