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 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
思路:
简单题目。注意格式。如果某一项系数为0,不输出。最后一种情况,没有存在项,输出"0\n"
源代码:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main() {
int na,nb;
scanf("%d",&na);
int flag[]={};
double res[]={};
int index;double data;
for(int i=;i<na;++i) {
scanf("%d %lf",&index,&data);
flag[index]=;
res[index]+=data;
}
scanf("%d",&nb);
for(int i=;i<nb;++i) {
scanf("%d %lf",&index,&data);
flag[index]=;
res[index]+=data;
}
int cnt=;
for(int i=;i<=;++i) {
if(flag[i]&&fabs(res[i])>1e-)
cnt++;
}
if(cnt==) {
printf("");
} else {
printf("%d ",cnt);
}
for(int i=;i>=;--i) {
if(flag[i]&&fabs(res[i])>1e-) {
printf("%d %.1lf",i,res[i]);
cnt--;
if(cnt==) {
break;
} else {
printf(" ");
}
}
}
printf("\n");
return ;
}
PAT 1002. A+B for Polynomials (25) 简单模拟的更多相关文章
- 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(map模拟)
This time, you are supposed to find A+B where A and B are two polynomials(多项式). Input Each input fil ...
- PAT 甲级1002 A+B for Polynomials (25)
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
- 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)(25 分)
1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...
- PAT甲 1002. A+B for Polynomials (25) 2016-09-09 22:50 64人阅读 评论(0) 收藏
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
- 1002 A+B for Polynomials (25)(25 point(s))
problem 1002 A+B for Polynomials (25)(25 point(s)) This time, you are supposed to find A+B where A a ...
随机推荐
- jQuery+Ajax+PHP实现异步分页数据显示
这几天做毕业设计的时候需要使用到一个异步分页的功能,即翻页的时候只是刷新分页的数据而不是刷新整个页面.因为之前做项目的时候没有做过这方面的功能,所以还是纠结了挺长时间的,在网上也找了很多资料,结合自己 ...
- FastDFS与Nginx的配置说明
1.简介 FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载 ...
- win8在安装office visio2003出现“请求的操作需要提升”,解决方法
单击右键,然后以管理员身份运行即可
- 版本控制之五:SVN trunk(主线) branch(分支) tag(标记) 用法详解和详细操作步骤(转)
使用场景: 假如你的项目(这里指的是手机客户端项目)的某个版本(例如1.0版本)已经完成开发.测试并已经上线了,接下来接到新的需求,新需求的开发需要修改多个文件中的代码,当需求已经开始开发一段时间的时 ...
- Leetcode题解(五)
17.Letter Combinations of a Phone Number 题目 针对输入的数字串,每一个数字都对应对个字符可以选择.因此可以直接采用递归的思想,依次遍历数字串的每一个数字,处理 ...
- Linux 新建文件/文件夹,删除文件文件夹,查找文件 打开文件
1.新建文件夹:mkdir xx 2.新建文件: touch 1.py 3.删除文件/文件夹: rm -r xx rm 1.py 4.打开文件:cat 1.py 只显示前几行 :head -2 1. ...
- 浅谈JavaScript的apply和call语句
我们试图在回调函数中,用this表示oDiv对象,这样感觉爽. 1 animate(oDiv,{"left":600},2000,function(){ 2 t ...
- mysql数据库事务详细剖析
在写之前交代一下背景吧! 做开发也好久了,没怎么整理过知识,现在剖析一下自己对数据库事务的认识,以前用sqlserver,现在转java后又用mysql.oracle.我这块就主要解释一下mysql数 ...
- python基础阶段 经典练习题 拾英札记(2)
因为编程的练习题是交互式的,在不断调试和不断渐进完善中,你会有一种成就感和快乐感,不断的修缮,不断的尝试. 其实,认知自己,和探索世界,也是这样的啊. 只要不放弃,要坚持. #7 根据列表lt,实现 ...
- [转载] Java学习之Hessian通信基础
转载自http://blog.sina.com.cn/s/blog_7f73e06d0100xn9j.html 一.首先先说Hessian是什么? Hessian:hessian是一个轻量级的r ...