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 ...
随机推荐
- 《Web Development with Go》Mangodb插入struct数据
学习数据持久化. package main import ( "fmt" "log" "time" "gopkg.in/mgo.v ...
- ios 免费抓包工具Stream
ios 免费抓包工具Streamhttps://www.52pojie.cn/thread-1002406-1-1.html
- 进制转换器V1.0_Beta
一.截图部分 二.代码部分: char2num() 作用:将字符转化成对应的数字 e.g. '9'->9 'A'->10 int char2num(char ch) ...
- 2019-2020-1 20199305《Linux内核原理与分析》第十一周作业
ShellShock 攻击实验 (一)何为ShellShock? 2014年9月24日,Bash中发现了一个严重漏洞shellshock,该漏洞可用于许多系统,并且既可以远程也可以在本地触发,这项漏洞 ...
- selenium-server-standalone下载网站
http://selenium-release.storage.googleapis.com/index.html
- java之封装
java中通过将成员变量声明为private,再提供公共的public方法:setXxx()和getXxx()实现对该属性的操作,以实现以下目的: 隐藏一个类中不需要对外提供的实现: 使用者只能通过事 ...
- Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类
因为Jdk7及以前的日期时间类的不方便使用问题和线程安全问题等问题,2005年,Stephen Colebourne创建了Joda-Time库,作为替代的日期和时间API.Stephen向JCP提交了 ...
- Add a Simple Action添加简单按钮
In this lesson, you will learn how to create a Simple Action. For this purpose, a new View Controlle ...
- 高强度学习训练第十四天总结:HashMap
HashMap 简介 HashMap 主要用来存放键值对,它基于哈希表的Map接口实现,是常用的Java集合之一. JDK1.8 之前 HashMap 由 数组+链表 组成的,数组是 HashMap ...
- Unitest自动化测试基于HTMLTestRunner报告案例
报告效果如下: HTMLTestRunner脚本代码如下: #coding=utf-8 # URL: http://tungwaiyip.info/software/HTMLTestRunner.ht ...