poj 多项式加法 map
来源:互联网 发布:养猫还是养狗 知乎 编辑:程序博客网 时间:2024/05/24 00:09
POJ 多项式加法
- 总时间限制:
- 1000ms
- 内存限制:
- 5000kB
- 描述
我们经常遇到两多项式相加的情况,在这里,我们就需要用程序来模拟实现把两个多项式相加到一起。 首先,我们会有两个多项式,每个多项式是独立的一行,每个多项式由系数、幂数这样的多个整数对来表示。
如多项式2x20- x17+ 5x9- 7x7+ 16x5+ 10x4+ 22x2- 15
对应的表达式为:2 20 -1 17 5 9 - 7 7 16 5 10 4 22 2 -15 0。
为了标记每行多项式的结束,在表达式后面加上了一个幂数为负数的整数对。
同时输入表达式的幂数大小顺序是随机的。
我们需要做的就是把所给的两个多项式加起来。
- 输入
- 输入包括多行。
第一行整数n,表示有多少组的多项式需要求和。 (1 < n < 100)
下面为2n行整数,每一行都是一个多项式的表达式。 表示n组需要相加的多项式。
每行长度小于300。 - 输出
- 输出包括n行,每行为1组多项式相加的结果。
在每一行的输出结果中,多项式的每一项用“[x y]”形式的字符串表示,x是该项的系数、y 是该项的幂数。 要求按照每一项的幂从高到低排列,即先输出幂数高的项、再输出幂数低的项。
系数为零的项不要输出。 /*map实现多项式的加法定义一个map int int 读入t*/ #include<map>#include<queue>#include<cmath>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#define ll long long#define inf 1000000000using namespace std;int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}int T,n,a,b;map<int,int> p;int main(){ T=read(); while(T--){ p.clear(); while(1){ a=read();b=read(); if(b<0)break; p[b]+=a; } while(1){ a=read();b=read(); if(b<0)break; p[b]+=a; } for(map<int,int>::reverse_iterator i=p.rbegin();i!=p.rend();i++) if(i->second)printf("[ %d %d ] ",i->second,i->first); puts(""); } return 0;}
阅读全文
0 0
- poj 多项式加法 map
- POJ 多项式加法
- POJ 多项式加法
- POJ.1.多项式加法
- POJ 多项式加法
- POJ 多项式加法
- POJ——多项式加法(WA)
- POJ——多项式加法(AC)
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- SpringMVC中的@RequestMapping
- Nginx提高并发量的简单思路
- 系统rhel7本地yum源的配置
- python_fullstack基础(二)
- Hybris入门详解一BY.Shaun-sheng
- poj 多项式加法 map
- yii2 遇到的小方法
- spring中读取property的几种方式及解析
- python︱apple开源机器学习框架turicreate中的SFrame——新形态pd.DataFrame
- Android:动态修改TextView文字内容
- 基于Unity3D的声音管理系统的实现
- UDP单播、广播和多播
- mysql 数据库在windows下安装
- 追求美好