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 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 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;
}