PAT1002: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 思路 题目要求打印两个多项式相加后的多项式的指数和系数。 用map<指数,系数>这样的关联形式来模拟相加就很简单了,找到对应的指数(键)计算对应的系数(值)就行。
特别注意的情况:指数的系数相加如果为0,那么这个指数和其对应的系数就不用输出了(相消了),而且对应的第一个输出的数字——指数个数也要减1。 代码
#include<iostream>
#include<map>
#include<iomanip>
#include<iterator>
using namespace std;
int main()
{
int k1,ksum = ;
while(cin >> k1)
{
map<int,double> sum; //<指数,系数>
for(int i = ;i < k1;i++)
{
int n;double a;
cin >> n >> a;
sum.insert(pair<int,double>(n,a));
ksum++;
} int k2;
cin >> k2;
for(int i = ;i < k2;i++)
{
int n;double a;
cin >> n >> a;
if(sum.count(n) > )
{
sum[n] += a;
if(sum[n] == )
ksum--;
}
else
{
sum.insert(pair<int,double>(n,a));
ksum++;
} } cout << ksum;
for(map<int,double>::reverse_iterator it = sum.rbegin(); it != sum.rend();it++)
{
if(it->second != )
{
cout << " " << it->first;
cout <<" "<< fixed << setprecision() << it->second;
} }
cout << endl;
}
}
PAT1002:A+B for Polynomials的更多相关文章
- pat1002. A+B for Polynomials (25)
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
- 1002. A+B for Polynomials (25)
题目链接:https://www.patest.cn/contests/pat-a-practise/1002 原题如下: This time, you are supposed to find A+ ...
- PAT (Advanced Level) Practise:1002. A+B for Polynomials
[题目链接] This time, you are supposed to find A+B where A and B are two polynomials. Input Each input f ...
- \(\S1 \) Gaussian Measure and Hermite Polynomials
Define on \(\mathbb{R}^d\) the normalized Gaussian measure\[ d \gamma(x)=\frac{1}{(2\pi)^{\frac{d}{2 ...
- 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 polynom ...
- Legendre polynomials
In mathematics, Legendre functions are solutions to Legendre's differential equation: In particular, ...
- PAT 解题报告 1009. Product of Polynomials (25)
This time, you are supposed to find A*B where A and B are two polynomials. Input Specification: Each ...
- 【PAT】1009. Product of Polynomials (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1009 分析:简单题.相乘时指数相加,系数相乘即可,输出时按指数从高到低的顺序.注意点:多项式相 ...
- PAT1002
This time, you are supposed to find A+B where A and B are two polynomials. 这一次,你被要求计算A+B当A和B是两个多项式的时 ...
随机推荐
- Leetcode_144_Binary Tree Preorder Traversal
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42876699 Given a binary tree, r ...
- Unity3D学习笔记(五)C#与JavaScript组件访问的比较
由于之前用JavaScript用的比较多,因此总是想用以前的方法来访问组件,却屡遭失败,经过查阅资料发现,二者存在较大的不同. 下面以调用3D Text组件HurtValue为例,来比较二者的不同 J ...
- Java-HttpSession
//session给用户一种标志,让用户可以在不同页面以及网站中都有一个特殊的标记 public interface HttpSession { /** * Returns the time when ...
- sql将查询结果建立为新表
1.sqlserver中,使用: select * into tab_new from tab_old SELECT * into anzhiresult from (select * from fa ...
- linux内核算法---hex_to_bin分享
这是我从内核抠出来的一段代码,用处就是传入一个字符,即可以用printf语句%d以十进制数的格式输出,同时也可以以%p地址的形式输出. 代码如下: #include <stdio.h> # ...
- 在SD/MMC卡上实现hive (Implement WinCE HIVE&ROM system on NAND or SD system )
本是个很简单的topic,但无奈的是很多客户都没有实现.所以只能写一个guide给客户,让他们依葫芦画瓢. 在SD卡上实现hive以及实现binfs最精髓的思想是,在boot stage 1依次加载s ...
- ZooKeeper leader election
Paxos是分布式应用中解决同步问题的核心.作为应用研发工程师,我们总是倾向于使用一种相对简洁的方式实现复杂的算法.ZooKeeper leader election实现就是一个非常好的参考. 其实现 ...
- Hadoop DataNode不能正常工作的原因
在把Hadoop环境搭建成功,并且也Hadoop的各个组件都正常工作.在重启过几次Hadoop后发现DataNode不能正常工作,打开Hadoop 的后台http://localhost:50030和 ...
- DTN学习,theONE模拟器网络相关资料整理
下面是一个百度空间的: http://hi.baidu.com/jensenliao 博客园的一篇博客:theONE模拟器简介(主要讲述,软件配置,软件结构) http://www.cnblogs.c ...
- The 1st tip of DB Query Analyzer
The 1st tip of DB Query Analyzer Ma Genfeng (Guangdong Unitoll Services incorporate ...