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

方法一:

程序设计:

  构造一个包含1001个元素的数组,把输入的多项式的指数项当做数组的下标,系数项当做该下标对应元素的值

  **注意:系数项求和之后为0的项不显示

C++ 代码如下:

 #include <bits/stdc++.h>
using namespace std; int main(){
float num[];
for(int i=;i<;i++){
num[i]=0.0;
}
int m,n,temp1,count=;
float temp2;
cin>>m;
for(int i=;i<m;i++){
cin>>temp1>>temp2;
num[temp1]+=temp2;
}
cin>>n;
for(int i=;i<n;i++){
cin>>temp1>>temp2;
num[temp1]+=temp2;
}
for(int i=;i<;i++){
if(num[i]!=)
count++;
}
cout<<count;
for(int i=;i>=;i--){
if(num[i]!=)
cout<<' '<<i<<' '<<setiosflags(ios::fixed)<<setprecision()<<num[i];
}
system("pause");
}

C++ Code 1

方法二:

程序设计:

  使用标准模板库(STL)中的 map 容器

  关于 map :map 是用来存放 <key,value>  键值对的数据结构,key 和 value 一一对应,并且在map 中,key是唯一的

            1.在map中存入数据时,默认安装key的升序来存放

map<int,float> num; //默认按照key的升序排列存储数据,int,float分别为key和value的数据类型

        此题中将多项式的指数项作为key,系数项作为value进行存储,若最后按照key的降序进行打印输出时,可使用逆向迭代器来遍历map中的元素:

map<int,float>::reverse_iterator it;  //定义map的逆向迭代器
for(it=num.rbegin();it!=num.rend();it++){
if(it->second!=0)
cout<<' '<<it->first<<' '<<setiosflags(ios::fixed)<<setprecision(1)<<it->second;
}        

        其中:num.rbegin() :返回指向第一个元素的逆向迭代器

           num.rend() :返回指向最后一个元素的逆向迭代器

              it->first :表示map中 it 迭代器指向的元素的key值,等同于 (*it).first

              it->second:表示map中 it 迭代器指向的元素的value值,等同于 (*it).second

        2.将map存入数据时按照key的降序排列

map<int,float,greater<int> > num;  //按照key的降序来存储数据
// ^ 注意此处有空格

        这时,再打印多项式的指数项和系数项时可使用map的普通的迭代器:

map<int,float>::iterator it;  //普通的迭代器
for(it=num.begin();it!=num.end();it++){
if(it->second!=0)
cout<<' '<<it->first<<' '<<setiosflags(ios::fixed)<<setprecision(1)<<it->second;
}

      

C++ 代码如下:

 #include <bits/stdc++.h>
using namespace std; int main(){
map<int,float,greater<int> > num;
int m,exp;
float coef;
for(int i=;i<;i++){
cin>>m;
for(int j=;j<m;j++){
cin>>exp>>coef;
num[exp]+=coef;
}
}
m=;
map<int,float>::iterator it;
for(it=num.begin();it!=num.end();it++){
if(it->second!=)
m++;
}
cout<<m;
for(it=num.begin();it!=num.end();it++){
if(it->second!=)
cout<<' '<<it->first<<' '<<setiosflags(ios::fixed)<<setprecision()<<it->second;
}
system("pause");
}

C++ Code 2 

【PAT】1002. A+B for Polynomials (25)的更多相关文章

  1. 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 ...

  2. PAT 甲级1002 A+B for Polynomials (25)

    1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...

  3. 【PAT】B1075 链表元素分类(25 分)

    这道题算有点难,心目中理想的难度. 不能前怕狼后怕虎,一会担心超时,一会又担心内存过大,直接撸 将三部分分别保存到vector 有意思的在于输出 分别输出第一个的add和num 中间输出nextadd ...

  4. 【PAT甲级】1002 A+B for Polynomials (25 分)

    题意:给出两个多项式,计算两个多项式的和,并以指数从大到小输出多项式的指数个数,指数和系数. AAAAAccepted code: #include<bits/stdc++.h> usin ...

  5. 【PAT】1002. 写出这个数 (20)

    1002. 写出这个数 (20) 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式 ...

  6. 【PAT】A1002 A+B for Polynomials

    仅有两个要注意的点: 如果系数为0,则不输出,所以输入结束以后要先遍历确定系数不为零的项的个数 题目最后一句,精确到小数点后一位,如果这里忽略了,会导致样例1,3,4,5都不能通过

  7. PAT Advanced 1002 A+B for Polynomials (25 分)(隐藏条件,多项式的系数不能为0)

    This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each ...

  8. 【PAT】B1080 MOOC期终成绩(25 分)

    还是c++好用,三部分输入直接用相同的方法, 用map映射保存学生在结构体数组中的下标. 结构体保存学生信息,其中期末成绩直接初始化为-1, 注意四舍五入 此题还算简单 #include<ios ...

  9. 【PAT】1005. 继续(3n+1)猜想 (25)

    1005. 继续(3n+1)猜想 (25) 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中 ...

随机推荐

  1. [转]从头开始 GAN

    1 前言 GAN的火爆想必大家都很清楚了,各种GAN像雨后春笋一样冒出来,大家也都可以名正言顺的说脏话了[微笑脸].虽然目前GAN的酷炫应用还集中在图像生成上,但是GAN也已经拓展到NLP,Robot ...

  2. 调整的R方_如何选择回归模型

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  3. 前端PHP入门-004-数据类型,特别需要注意字符串

    人类世界对万事万物都有种类划分,例如: 哺乳动物 人.猫.马.鸭嘴兽-.等等 蔬菜 西红柿.波菜.茄子-.等等 水果 西瓜.桃子.苹果-.等等 数据类型:就是对数据分类的一个划分而已 整型就是整数 我 ...

  4. NOIP 2000 方格取数

    https://www.luogu.org/problem/show?pid=1004 题目描述 设有N*N的方格图(N<=9),我们将其中的某些方格中填入正整数,而其他的方格中则放 人数字0. ...

  5. Python学习笔记(四十)— 内置模块(9)HTMLParser

    摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432002312 ...

  6. JAVA多线程提高二:传统线程的互斥与同步&传统线程通信机制

    本文主要是回顾线程之间互斥和同步,以及线程之间通信,在最开始没有juc并发包情况下,如何实现的,也就是我们传统的方式如何来实现的,回顾知识是为了后面的提高作准备. 一.线程的互斥 为什么会有线程的互斥 ...

  7. spring boot(二):注解大全

    spring boot注解 @Autowired 注解的意思就是,当Spring发现@Autowired注解时,将自动在代码上下文中找到和其匹配(默认是类型匹配)的Bean,并自动注入到相应的地方去. ...

  8. [bzoj 2460]线性基+贪心+证明过程

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2460 网上很多题目都没说这个题目的证明,只说了贪心策略,我比较愚钝,在大神眼里的显然的策略 ...

  9. 【CodeForces】708 C. Centroids 树的重心

    [题目]C. Centroids [题意]给定一棵树,求每个点能否通过 [ 移动一条边使之仍为树 ] 这一操作成为树的重心.n<=4*10^5. [算法]树的重心 [题解]若树存在双重心,则对于 ...

  10. jquery 中$符号六大作用

    jquery 中$符号六大作用 2012-12-16 86市场网 javascript a.$用作选择器, var e = $("h1 a"); var f = $("t ...