1002 A+B for Polynomials (25分)

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1​​ a​N​1​​ ​ N​2​​ a​N​2​​ … N​K​​ a​N​K 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≤N​K​​ <⋯<N2 <N​1​​ ≤1000.

Output Specification:
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

自己写完测试点2一直过不了,后来发现是while的一个判断在c++里报警告,将动态数组改为静态数组可以过,修改while判断条件也可以过,但我觉得我代码没问题。。。有问题的是c++的警告。
以及,看别人的好像都用1001的数组做,这样不浪费吗?还是因为有别的原因?比如代码量少,思路简单,写起来快?
出错代码

#include <iostream>
#include <iomanip>
#define MAXN 20
using namespace std;
int main() {
    int k1, k2;
    cin >> k1;
    int* n1 = new int[k1];
    float* a1 = new float[k1];
    for (int i = 0; i < k1; i++) {
        cin >> n1[i] >> a1[i];
    }
    cin >> k2;
    int* n2 = new int[k2];
    float* a2 = new float[k2];
    for (int i = 0; i < k2; i++) {
        cin >> n2[i] >> a2[i];
    }
    int n3[MAXN] = { 0 };
    float a3[MAXN] = { 0.0 };
    int i = 0, j = 0, k = 0;
    while (i < k1 || j < k2) {
        if (j == k2 || n1[i] > n2[j]) {
            n3[k] = n1[i];
            a3[k] = a1[i];
            i++;
            k++;
        }
        else if (i == k1 || n1[i] < n2[j]) {
            n3[k] = n2[j];
            a3[k] = a2[j];
            j++;
            k++;
        } else if (n1[i] == n2[j]) {
            if (a1[i] + a2[j] != 0) {
                n3[k] = n1[i];
                a3[k] = a1[i] + a2[j];
                i++;
                j++;
                k++;
            }
            else {
                i++;
                j++;
            }
        }
    }
    cout << k;
    cout.setf(ios::fixed);
    cout.precision(1);
    for (int i = 0; i < k; i++) {
        cout << " " << n3[i] << " " << a3[i];
    }
    return 0;
}

正确代码1

#include <iostream>
#include <iomanip>
#define MAXN 20
using namespace std;
int main() {
	int k1, k2;
	cin >> k1;
	int* n1 = new int[k1];
	float* a1 = new float[k1];
	for (int i = 0; i < k1; i++) {
		cin >> n1[i] >> a1[i];
	}
	cin >> k2;
	int* n2 = new int[k2];
	float* a2 = new float[k2];
	for (int i = 0; i < k2; i++) {
		cin >> n2[i] >> a2[i];
	}
	int n3[MAXN] = { 0 };
	float a3[MAXN] = { 0.0 };
	int i = 0, j = 0, k = 0;
	while (i < k1 && j < k2) {
		if (n1[i] > n2[j]) {
			n3[k] = n1[i];
			a3[k] = a1[i];
			i++;
			k++;
		}
		else if (n1[i] < n2[j]) {
			n3[k] = n2[j];
			a3[k] = a2[j];
			j++;
			k++;
		}
		else if (n1[i] == n2[j]) {
			if (a1[i] + a2[j] != 0) {
				n3[k] = n1[i];
				a3[k] = a1[i] + a2[j];
				i++;
				j++;
				k++;
			}
			else {
				i++;
				j++;
			}
		}
	}
	while (i < k1) {
		n3[k] = n1[i];
		a3[k] = a1[i];
		i++;
		k++;
	}
	while (j < k2) {
		n3[k] = n2[j];
		a3[k] = a2[j];
		j++;
		k++;
	}
	cout << k;
	cout.setf(ios::fixed);
	cout.precision(1);
	for (int i = 0; i < k; i++) {
		cout << " " << n3[i] << " " << a3[i];
	}
	return 0;
}

正确代码2

#include <iostream>
#include <iomanip>
#define MAXN 20
using namespace std;
int main() {
    int n1[MAXN] = { 0 };
    float a1[MAXN] = { 0.0 };
    int n2[MAXN] = { 0 };
    float a2[MAXN] = { 0.0 };
    int n3[MAXN] = { 0 };
    float a3[MAXN] = { 0.0 };
    int k1, k2;
    cin >> k1;
    for (int i = 0; i < k1; i++) {
        cin >> n1[i] >> a1[i];
    }
    cin >> k2;
    for (int i = 0; i < k2; i++) {
        cin >> n2[i] >> a2[i];
    }
    int i = 0, j = 0, k = 0;
    while (i < k1 || j < k2) {
        if (j == k2 || n1[i] > n2[j]) {
            n3[k] = n1[i];
            a3[k] = a1[i];
            i++;
            k++;
        }
        else if (i == k1 || n1[i] < n2[j]) {
            n3[k] = n2[j];
            a3[k] = a2[j];
            j++;
            k++;
        } else if (n1[i] == n2[j]) {
            if (a1[i] + a2[j] != 0) {
                n3[k] = n1[i];
                a3[k] = a1[i] + a2[j];
                i++;
                j++;
                k++;
            }
            else {
                i++;
                j++;
            }
        }
    }
    cout << k;
    cout.setf(ios::fixed);
    cout.precision(1);
    for (int i = 0; i < k; i++) {
        cout << " " << n3[i] << " " << a3[i];
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值