我是靠谱客的博主 友好小丸子,最近开发中收集的这篇文章主要介绍PAT 甲级 10021002. 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

注意去除相加后为零的项,输出记得要保留小数点儿后一位,格式错误记得加空格 cout<<" "<<i<<" "<<s[i];

#include<iostream>
#include<iomanip>
using namespace std;

int main() {
	int a = 0;
	float p[1001] = { 0 }, q[1001] = { 0 }, s[1001] = { 0 }, k = 0, num = 0;
	cin >> num;
	for (int i = 0; i < num; i++) {
		cin >> a;
		cin >> p[a];
	}
	cin >> num;
	for (int i = 0; i < num; i++) {
		cin >> a;
		cin >> q[a];
	}
	for (int i = 0; i<1001; i++) {
		if (p[i] != 0 && q[i] != 0) {
			s[i] = p[i] + q[i];
			if (s[i])k++;
			continue;
		}
		if (p[i] != 0) {
			s[i] = p[i];
			k++;
			continue;
		}
		if (q[i] != 0) {
			s[i] = q[i];
			k++;
			continue;
		}
	}
	cout << k;
	for (int i = 1000; i >= 0; i--) {
		if (s[i] != 0) {
			cout << " " << i << " ";
			cout << setiosflags(ios::fixed);
			cout << setprecision(1) << s[i];
		}
	}
	cout << endl;
	return 0;
}

最后

以上就是友好小丸子为你收集整理的PAT 甲级 10021002. A+B for Polynomials (25)的全部内容,希望文章能够帮你解决PAT 甲级 10021002. A+B for Polynomials (25)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部