20分代码,没有找出错误,望有大神指正,不胜感激!!!
//1009. Product of Polynomials(25)
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int K1, K2;
int count = 0;
float a[1010], b[1010];
float co[2020] = { 0.0 };
for (int i = 0; i < 1010; i++) {
a[i] = -1.0;
b[i] = -1.0;
}
cin >> K1;
for (int i = 0; i < K1; i++) {
int n;
float an;
cin >> n >> an;
a[n] = an;
}
cin >> K2;
for (int j = 0; j < K2; j++) {
int n;
float an;
cin >> n >> an;
b[n] = an;
}
for (int i = 0; i < 1010; i++) {
if(a[i] != -1.0){
for (int j = 0; j < 1010; j++) {
if (b[j] != -1.0) {
co[i + j] += a[i] * b[j];
}
}
}
}
for (int i = 0; i < 2020; i++) {
if (co[i] != 0)
count++;
}
cout << count << " ";
for (int i = 2019; i >=0; i--) {
if (co[i] != 0) {
cout << i << " " << fixed << setprecision(1) << co[i];
count--;
if (count != 0)
cout << " ";
}
}
cout << endl;
return 0;
}
完全正确代码
//1009. Product of Polynomials(25)
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int K1, K2;
int count = 0;
float a[1010] = { 0.0 }, b[1010] = { 0.0 };
float co[2020] = { 0.0 };
cin >> K1;
for (int i = 0; i < K1; i++) {
int n;
float an;
cin >> n >> an;
a[n] = an;
}
cin >> K2;
for (int j = 0; j < K2; j++) {
int n;
float an;
cin >> n >> an;
b[n] = an;
}
for (int i = 0; i < 1010; i++)
for (int j = 0; j < 1010; j++)
co[i + j] += a[i] * b[j];
for (int i = 0; i < 2020; i++) {
if (co[i] != 0)
count++;
}
cout << count << " ";
for (int i = 2019; i >=0; i--) {
if (co[i] != 0) {
cout << i << " " << fixed << setprecision(1) << co[i];
count--;
if (count != 0)
cout << " ";
}
}
cout << endl;
return 0;
}
这道简单题我脑残的花了超级长时间,期间差点要炸!