1002. A+B for Polynomials (25)
来源:互联网 发布:js done事件 编辑:程序博客网 时间:2024/05/04 08:51
主页
题目集
基本信息
题目列表
提交列表
排名
帮助
1002. A+B for Polynomials (25)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
解题思路:
1.哈希法保存两多项式。并对其做标记
2.根据标记进行加法,对结果进行哈希保存,并做标记。要注意和为零的情况。
3.从数组尾部根据标记遍历数组,保证从大到小输出。
示例代码:
#define _CRT_SECURE_NO_WARNINGS#include<iostream>#include<cstdio>using namespace std;const int MAX = 1002;bool flag1[MAX];bool flag2[MAX];bool flag3[MAX];int main(){ float a[MAX]; float b[MAX]; int n1,n2,exp,n3=0; float coe; cin >> n1; for (int i = 0; i < n1; i++) { cin >> exp >> coe; a[exp] = coe; flag1[exp] = true; } cin >> n2; for (int i = 0; i < n2; i++) { cin >> exp >> coe; b[exp]= coe; flag2[exp] = true; } float sum[MAX]; for (int i = 0; i < MAX; i++) { if (flag1[i] && flag2[i]) { sum[i] = a[i] + b[i]; } else if (flag1[i]) { sum[i] = a[i]; } else if (flag2[i]) { sum[i] = b[i]; } if (!(flag1[i] == false && flag2[i] == false)) { if (sum[i] != 0.0f) { flag3[i] = true; n3++; } } } cout << n3; for (int i = MAX - 1; i >= 0; i--) { if (flag3[i]) printf(" %d %.1f",i,sum[i]); } system("pause"); return 0;}
- 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- 【PAT】1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- PAT 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- [PAT]1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- pat 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- PAT 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)
- 基于AngularJS+nodeJS的单页web的SEO处理
- 基础学习笔记之opencv(24):imwrite函数的使用
- java并发包消息队列
- 关于django-channels的token认证实现
- Android进阶之AlertDialog自定义
- 1002. A+B for Polynomials (25)
- 2017.01.21 明天做的
- 接口限流算法
- 2n皇后问题
- leetcode 476. Number Complement题解
- IO—stream
- 内部类
- SRM 548 KingdomAndPassword
- PAT(甲级)Hello World for U (20)