PAT :计算机程序设计能力考试:一个高校编程学习赛,内容基础,据说题目描述含糊不清,造成诸多理解错误。
第一观感是:输入输出样例极少,未给学生充分理解题目,提供更多辅助。
问题描述
This time, you are supposed to find A+B where A and B are two polynomials.
求多项式 A + B A + B A+B 的和。
输入格式
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N 1 a N 1 N 2 a N 2 . . . N k a N k KN_1a_{N1}N_2a_{N2}...N_ka_{Nk} KN1aN1N2aN2...NkaNk
where K is the number of nonzero terms in the polynomial, N i N_i Ni and a N i a_{Ni} aNi ( i = 1 , 2 , ⋯ , K ) (i=1,2,⋯,K) (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1 ≤ K ≤ 10 1≤K≤10 1≤K≤10, 0 ≤ N k < . . . < N 2 < N 1 ≤ 1000 0≤N_k<...<N_2<N_1≤1000 0≤Nk<...<N2<N1≤1000.
两行输入,每行输入描述一个多项式,包括:多项式的非零项的项数 K ( 1 ≤ K ≤ 10 ) (1≤K≤10) (1≤K≤10),以及每一项的指数 N i N_i Ni ( 0 ≤ N k < . . . < N 2 < N 1 ≤ 1000 ) (0≤N_k<...<N_2<N_1≤1000) (0≤Nk<...<N2<N1≤1000) 和系数 a N i a_{Ni} aNi,使用空格分割。
输出格式
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.
一行输出, 多项式 
    
     
      
       
        A
       
       
        +
       
       
        B
       
      
      
       A + B
      
     
    A+B 的和,格式和输入一样。注意:行尾不可有多余空格,系数保留一位小数。
输入输出样例
| 输入样例 | 输出样例 | 
|---|---|
| 2 1 2.4 0 3.2 2 2 1.5 1 0.5 | 3 2 1.5 1 2.9 0 3.2 | 
样例解释:无。
题解 1
思路分析:请看注释。【坑点:格式化输出】
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
	
	double n[1001] = {0};	// 多项式的各项系数
	int k, z;	// k 多项式项数,z 指数
	double x;	// x 系数
	// 分别读取两个多项式 A、B,并对相同指数的项,系数求和
	for(int i = 0; i < 2; i++) {
		cin >> k;
		while(k--) {
			cin >> z >> x;
			n[z] += x;
		}
	}
	
	// 统计 A + B 和的非零项个数
	int TK = 0;
	for(int i = 1000; i >= 0; i--) {
		if(n[i] != 0) {
			TK ++;
		}
	}
	// 系数保留 1 位小数
	cout << fixed << setprecision(1);
	// 格式化输出
	cout << TK;
	for(int i = 1000; i >= 0; i--) {
		if(n[i] != 0) {
			cout << " " << i << " " << n[i];
		}
	}
	return 0;
}










