PAT 1002 A+B for Polynomials(map模拟)
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
题目意思:两个多项式的相加,每一行第一个数k表示非零系数的个数,之后是每个非零系数的指数和系数。
解题思路:可以直接使用数组来模拟,这里我使用map复习一下map的用法。
https://www.cnblogs.com/wkfvawl/p/9387566.html
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
int main()
{
int T=;
int n,k,i;
double m;
int cnt=;
map<int,double>mp;
map<int,double>::iterator it;//迭代器
map<int,double>::reverse_iterator rit;//反向迭代器
while(T--)
{
scanf("%d",&k);
for(i=; i<k; i++)
{
scanf("%d%lf",&n,&m);//n为指数,m为系数
mp[n]+=m;
}
}
for(it=mp.begin();it!=mp.end();it++)//系数为0的
{
if(it->second!=)
{
cnt++;
//printf("%lf\n",it->second);
}
}
printf("%d",cnt);
//map默认从小到大,可以用逆向迭代器逆序输出
for(rit=mp.rbegin();rit!=mp.rend();rit++)//系数为0的
{
if(rit->second!=)
{
printf(" %d %.1f",rit->first,rit->second);
}
}
printf("\n");
return ;
}
PAT 1002 A+B for Polynomials(map模拟)的更多相关文章
- 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 ...
- PAT 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 con ...
- PAT 1002 A+B for Polynomials (25分)
题目 This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: E ...
- PAT 1002. A+B for Polynomials
思路:就是两个多项式做加法–指数相同的相加即可,输出的时候按照指数递减输出,并且系数为0的项不输出. AC代码 #include <stdio.h> #include <vector ...
- PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642
PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...
- 【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 分)
1002 A+B for Polynomials (25 分) This time, you are supposed to find A+B where A and B are two polyno ...
- 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 ...
随机推荐
- 21.决策树(ID3/C4.5/CART)
总览 算法 功能 树结构 特征选择 连续值处理 缺失值处理 剪枝 ID3 分类 多叉树 信息增益 不支持 不支持 不支持 C4.5 分类 多叉树 信息增益比 支持 ...
- Docker 运行应用程序
一.docker run 镜像 执行的命令 docker run组合来运行一个容器:镜像文件不存在的话,会自动拉取(下载): 下图执行的命令的具体意思:docker 以ubuntu:15.10镜像创建 ...
- day100_12_4DataFrame和matplotlib模块
一.Dataframe的分组. 再网页表格数据 的分析中,可以使用以下语句进行爬取表格. res = pd.read_html('https://baike.baidu.com/item/NBA%E6 ...
- 更改路由器的外网IP
此方法适用于通过路由器拨号上网的宽带,若宽带通过光猫拨号上网则需要将光猫改为桥接模式并在路由器中配置宽带账号和密码 测试环境: 路由器:TP-LINK TL-WDR7800千兆版 硬件版本:1.0 软 ...
- 高阶函数&&高阶组件(二)
高阶组件总共分为两大类 代理方式 操纵prop 访问ref(不推荐) 抽取状态 包装组件 继承方式 操纵生命周期 操纵prop 代理方式之 操纵prop 删除prop import React fro ...
- 3万字长文概述:通俗易懂告诉你什么是.NET?什么是.NET Framework?什么是.NET Core?
[转载]通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core? 什么是.NET?什么是.NET Framework?本文将从上往下,循序渐进的介绍一系列相关.NET的 ...
- C#关于MySQL中文乱码问题
本人在写一个测试demo的时候,遇到一个问题就是添加的中文数据在数据库定义的明明是varchar类型,但是显示出来还是乱码,不过还是自己粗心导致的问题. 以下三种方式可以自查一下: 1. 首先检查 ...
- java基础(4):引用数据类型、流程控制语句
1. 引用数据类型 1.1 Scanner类 我们要学的Scanner类是属于引用数据类型,我们先了解下引用数据类型. 引用数据类型的使用: 与定义基本数据类型变量不同,引用数据类型的变量定义及赋值有 ...
- 理解并运用TP5.1-Facade
1.内容介绍 深入解析tp5.1与laravel 中Facade底层原理实现 1. 什么是Facade 2. 为什么需要有什么好处 3. Facade实现原理 4. 功能实现. 5. 容器注入 2. ...
- SSH框架之Hibernate第一篇
1.2Hibernate的概述: 1.2.1 什么Hibernate? Hibernate(开发源代码的对象关系映射框架)是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它 ...