[PAT A1009]Product of Polynomials
题目描述
1009 Product of Polynomials (25 分)This time, you are supposed to find A×B where A and B are two polynomials.
输入格式
输出格式
For each test case you should output the product 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 up to 1 decimal place.
输入样例
2 1 2.4 0 3.2
2 2 1.5 1 0.5
输出样例
3 3 3.6 2 6.0 1 1.6
解析
简单题,题意是给两个多项式,要我们计算多项式乘积,这里最需要注意的就是结果数组需要开到2000位,因为每一个最高次数为1000,乘积的最高次数为2000,切记切记!否则就会有好几组样例过不了。
#include<iostream>
using namespace std;
double num1[1010] = {
0 }, ans[2010] = {
0 };
int main()
{
int n;int cnt = 0;scanf("%d", &n);for (int i = 0; i < n; i++) {
int a;double b;scanf("%d %lf", &a, &b);num1[a] = b;}scanf("%d", &n);for (int i = 0; i < n; i++) {
int a;double b;scanf("%d %lf", &a, &b);for (int j = 0; j < 1010; j++) {
if (num1[j] != 0) {
ans[j + a] += b*num1[j];}}}for (int i = 2000; i >= 0; i--)if (ans[i] != 0.0) cnt++;printf("%d ", cnt);for (int i = 2009; i >= 0; i--) {
if (ans[i] != 0) {
printf("%d %.1f", i, ans[i]);cnt--;if (cnt != 0) printf(" ");}}return 0;
}