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插入map,slice,Embedded Documents
这几个好理解, 更好的实现,再说. package main import ( "fmt" "log" "time" "gopkg ...
- mysql数据库基础SQL语句总结篇
常用的sql增删改查语句 创建数据库:create database db_name character set utf8;删除数据库:drop database db_name;切换数据库:use ...
- 解决IDEA中的DashBoard 不显示端口号
第一步:找到.idea目录下的workspace.xml文件 第二步:找到下图位置 并添加红色部分代码 添加代码如下: <option name="configurationTypes ...
- UI 自动化框架设想
测试框架选型: 首先,通过利用TestNG结合csv的使用,将测试用例数据转化为测试代码中的数据,减少了测试人员录入数据和准备数据的工具: 再次,通过对appium的封装,按照面向对象的思想将测试中用 ...
- netty源码解析(4.0)-27 ByteBuf内存池:PoolArena-PoolThreadCache
前面两章分析的PoolChunk和PoolSubpage,从功能上来说已经可以直接拿来用了.但直接使用这个两个类管理内存在高频分配/释放内存场景下会有性能问题,PoolChunk分配内存时算法复杂度最 ...
- Linux gzip: stdin: not in gzip format
在解压tar.gz文件的时候报错 tar -zxvf otp_src_18.3.tar.gz gzip: stdin: not in gzip format tar: Child returned s ...
- C# OpenCVSharp使用SetMouseCallback设置回调方法,报错"向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们。"
一.描述问题 当非托管代码调用托管应用程序的委托的时候,经常会报错“托管调试助手 "CallbackOnCollectedDelegate" Message=托管调试助手 &q ...
- javaWeb技术第一篇之HTML
<!-- 当前是最简的html --> <html> <!-- 告诉浏览器当前是一个html文档 最外面的标签. --> <head> <!--h ...
- PostgreSQL update set from 两表联合更新,注意与其它数据库更新语法有差别
最近用PostgreSql数据库进行表关联更新时,发现与之前用的Sql Server 和My Sql语法有很大差别,稍微不注意,很容易出错. PostgreSql表更新时,两个表只允许一个表起别名,一 ...
- 数据库在线导出工具:Adminer
数据库在线导出工具:Adminer Adminer是一个类似PhpMyAdmin的MySQL客户端的"页面PHP",它只有一个PHP文件,包括:数据库的普通和函数操作等功能,是一个 ...