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 ...
随机推荐
- jQuery删除节点和追加节点
for (var i in checkedBoxIds) { var $td = $("#" + checkedBoxIds[i]).parent().parent().detac ...
- zendstudio文件编码修改问题
转载:http://blog.csdn.net/kunlong0909/article/details/7818620 朋友,在zendstudio ide中,你是否碰到导入一个项目后,发现项目中文件 ...
- DB2导入导出数据库数据
导出数据库中数据 在db2cmd命令下生成建库脚本(-z指定模式名) db2look -d BBS -z db2admin -u db2admin -e -o bbs.sql 在db2cmd命令下导出 ...
- git merge git pull时候遇到冲突解决办法git stash
在使用git pull代码时,经常会碰到有冲突的情况,提示如下信息: error: Your local changes to 'c/environ.c' would be overwritten b ...
- SQL Server(一)——数据库基础知识
SQL:Structured Quety Language SQL SERVER是一个以客户/服务器(c/s)模式访问.使用Transact-SQL语言的关系型数据库管理子系统(RDBMS) DBMS ...
- Mongodb Manual阅读笔记:CH9 Sharding
9.分片(Sharding) Mongodb Manual阅读笔记:CH2 Mongodb CRUD 操作Mongodb Manual阅读笔记:CH3 数据模型(Data Models)Mongodb ...
- 带进度条的 jQuery 文件拖放上传插件
jQuery File Uploader :jQuery File Uploader 是一个 jQuery 文件拖放上传插件 兼容性判断 下载:https://github.com/danielm/u ...
- x01.Game.CubeRun: 风一样的女子
1.题解 小孩学英语比较有意思,Monkey three => 猴三,风一样的女子 => 风 Girl.诸如此类不是重点,重点是一花一世界,一草一天堂.花花草草,纷纷扰扰.大千世界,当别具 ...
- 简易的GCC图形界面GCCUI
这个 GCCUI.EXE 是配合上一篇博文<用VC6开发嵌入式LINUX程序>说的:用VC6辅助开发LINUX程序的时候使用.把 gcc 编译器增加一个简易的图形界面,可以自动读取 vc6 ...
- 安装rpm包
下载好一个rpm包怎样安装? [root@localhost ~]# ls anaconda-ks.cfg install.log install.log.syslog jboss-as-7.1.1- ...