[PAT A1009]Product of Polynomials

[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;
}
水平有限,如果代码有任何问题或者有不明白的地方,欢迎在留言区评论;也欢迎各位提出宝贵的意见!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值