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 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 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (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
anwser
#include<iostream>
#include<stdio.h>
#include<iomanip>
#include<cstring>
int main()
{
	freopen("test.txt", "r",stdin);
	float n[1001], n1[1001], n2[1001];
	memset(n, 0, 1001*sizeof(float));
	memset(n1, 0, 1001*sizeof(float));
	memset(n2, 0, 1001*sizeof(float));
	int a, b;
	std::cin>>a;
	for(int i = 0; i < a; i++){
		int temp1;
		float temp2;
		std::cin>>temp1>>temp2;
		n1[temp1] = temp2;
	}
	std::cin>>b;
	for(int i = 0; i < b; i++){
		int temp1;
		float temp2;
		std::cin>>temp1>>temp2;
		n2[temp1] = temp2;
	}
	int c = 0;
	for(int i = 0; i < 1001; i++){
		n[i] = n1[i] + n2[i];
//		std::cout<<n[i]<<i<<std::endl;
		if(n[i] != 0) c++;
	}
	std::cout<<c;
	for(int i = 1000; i >= 0; i--){
		if(n[i] != 0) {
			std::cout<<" "<<i<<" ";
			std::cout<<std::fixed<<std::setprecision(1)<<n[i];
//			printf("%.1f", n[i]);
		}
	}
	return 0;
}
/*
2 1 2.4 0 3.2
2 2 1.5 1 0.5
*/
experience
- 注意头文件名
- 注意边界条件以及输出格式
单词复习:
- polynomials 多项式
- exponents 范例,指数
- coefficients 系数
- respectively 分别的
- accurate 精确的
- decimal 小数
1002 A+B for Polynomials (25)(25 point(s))的更多相关文章
- 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 ... 
- A1002 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 甲级 1009 Product of Polynomials (25)(25 分)(坑比较多,a可能很大,a也有可能是负数,回头再看看)
		1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are two ... 
- A1009 Product of Polynomials (25)(25 分)
		A1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are tw ... 
- MySQL5.7.25(解压版)Windows下详细的安装过程
		大家好,我是浅墨竹染,以下是MySQL5.7.25(解压版)Windows下详细的安装过程 1.首先下载MySQL 推荐去官网上下载MySQL,如果不想找,那么下面就是: Windows32位地址:点 ... 
- PAT 甲级 1006 Sign In and Sign Out (25)(25 分)
		1006 Sign In and Sign Out (25)(25 分) At the beginning of every day, the first person who signs in th ... 
- 【PAT】1020 Tree Traversals (25)(25 分)
		1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ... 
- 【PAT】1052 Linked List Sorting (25)(25 分)
		1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ... 
- 【PAT】1060 Are They Equal (25)(25 分)
		1060 Are They Equal (25)(25 分) If a machine can save only 3 significant digits, the float numbers 12 ... 
随机推荐
- springboot+mybatis使用PageHelper分页
			项目结构和spring搭建mybatis请参考springboot整合mybatis.在这个基础上配置分页. 一:导入PageHelper依赖 <dependency> <group ... 
- 20165320 实验一 java环境的熟悉
			实验内容与步骤 一.java开发环境的熟悉 1.建立一个有关自己学号的目录 2.在当前文件下编译一个带包Hello.java文件 3.代码内容 package sq; import java.util ... 
- Python——列表的操作
			列表的操作:详细+易出错假设有两个列表: list1 = [1,2,3] list2 = ['a','b','c']列表的操作: 1.list.append() append只接受一 ... 
- Git 管理本地代码【转】
			转自:http://www.cnblogs.com/JessonChan/archive/2011/03/16/1986570.html 以前用SVN,不过没有用出感情来:倒是用出不少怨恨:由于没有很 ... 
- 数论-求n以内的质数
			一.埃拉托斯特尼筛法 名字很高大上,然而并没有什么卵用…… 思路: 在把<=√n的质数所有的<=n的倍数剔除,剩下的就都是质数了,很容易理解…… 复杂度O(nloglogn) #inclu ... 
- Unity教程之-Unity3d中针对Android Apk的签名验证(C#实现)
			当Unity开发的游戏以Android Apk的形式发布之后,经常会遇到的一种情况就是别人对我们的游戏进行二次打包,也就是用他们的签名替换掉我们的签名,从而堂而皇之的将胜利果实占为己有.面对这样的情况 ... 
- 17  Go Slices: usage and internals  GO语言切片: 使用和内部
			Go Slices: usage and internals GO语言切片: 使用和内部 5 January 2011 Introduction Go's slice type provides a ... 
- python 写入execl记录
			记录代码中关于写execl的操作 # 创建execl workbook = xlwt.Workbook(encoding='utf8') # 创建样式实例 style = xlwt.XFStyle() ... 
- pycharm tornado 项目 配置
			ycharm 配置tornado项目 使得能够像django项目一样运行 
- java 多线程总结篇1之——基本概念
			1.什么是线程 进程:每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1--n个线程.(进程是资源分配的最小单位) 线程:同一类线程共享代码和数据空间,每个线程 ... 
