当前位置: 代码迷 >> 综合 >> [PAT A1009]Product of Polynomials
  详细解决方案

[PAT A1009]Product of Polynomials

热度:35   发布时间:2023-12-15 06:17:47.0

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

水平有限,如果代码有任何问题或者有不明白的地方,欢迎在留言区评论;也欢迎各位提出宝贵的意见!

  相关解决方案