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 ...
随机推荐
- Java之Lambda表达式
函数式编程思想概述 面向对象过分强调“必须通过对象的形式来做事情”,而函数式思想则尽量忽略面向对象的复杂语法——强调做什么,而不是以什么形式做. 面向对象的思想: 做一件事情,找一个能解决这个事情的对 ...
- typescript与nodejs(一)最简单的webserver
安装nodejs tsc cnpm vscode 这些略 如果网络慢,可以考虑使用CNPM 一. 基本WebServer模块环境 1. 命令行 npm init 初始化一个目录为nodejs项目 2 ...
- IT兄弟连 HTML5教程 HTML5表单 新增的表单属性2
5 height和width属性 height和width属性规定用于image类型和input标签的图像高度和宽度.图像通常会同时指定高度和宽度属性.如果图像设置高度和宽度,图像所需的空间在加载页 ...
- Ubuntu 合上笔记本不会进入休眠模式
首先 sudo vim /etc/systemd/logind.conf 把文件中的 #HandleLidSwitch=suspend 修改成 HandleLidSwitch=ignore 重启服务 ...
- CSS3动画的使用
0921自我总结 CSS3动画的使用 一.动画的创建 @keyframes规则是创建动画 浏览器兼容 1.@keyframes myfirst 2.@-webkit-keyframes myfirst ...
- Create a Solution using the Wizard 使用向导创建解决方案
In this lesson, you will learn how to create a new XAF solution. You will also be able to run the ge ...
- bay——RAC 表空间时数据文件误放置到本地文件系统-介质恢复.txt
RAC添加新表空间时数据文件误放置到本地文件系统的修正 于是我想11G 也兼容这些操作的方法,但是11G的新特性有一点就是可以直接支持ASM文件系统直接可以和本地文件系统进行文件的拷贝了,也就是有三种 ...
- Linux—软连接与硬连接
软链接的创建,删除,修改 创建软链接:ln -s[目标文件或目录][软链接地址] 解释:软链接地址相当于快捷方式,目标文件或目录才是真正的内容.[软链接地址]指“快捷键”文件名称,该文件是被指令创建的 ...
- layui 获取radio单选框选中的值
Layui 获取 radio的值,layui判断radio选中的单选值 layui form 表单获取radio选中的值 首先准备两个radio <input type="radio& ...
- いくnotepad++
再见!Notepad++,好走不送! 1No zuo No Die 上周就发现Notepad++开发者在作妖,新版本放了个啥恶心的标注上来,本来想直接发文说一说,后来想想是不是这样又给它做了宣传,就决 ...