天天看點

[PAT A1009]Product of Polynomials[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.

輸入格式

[PAT A1009]Product of Polynomials[PAT A1009]Product of 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;
}
           

水準有限,如果代碼有任何問題或者有不明白的地方,歡迎在留言區評論;也歡迎各位提出寶貴的意見!

繼續閱讀