joj1013 Polynomial Multiplication

 1013: Polynomial Multiplication


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K4660807Standard

A polynomial is a sum of terms, where each term is a coefficient multiplied by the variable x raised to some nonnegative integer power. The largest such power is referred to as the degree of the polynomial. For example: x^3+x^2+x+1, x^3+x, x^2, and 1 are all polynomials.

Input


A positive integer denoting the number of cases.
(The degree of polynomial 1) followed by the value of each coefficient for polynomial 1.
(The degree of polynomial 2) followed by the value of each coefficient for polynomial 2.
No polynomial will have degree greater than five. All coefficients will be greater than or equal to zero and less than 65536. Each polynomial will be on a separate line. A blank line will separate each case.

Output


The product of polynomial 1 and polynomial 2. The caret symbol (^) should be used to denote exponentiation. That is, x^2 means x*x. But, instead of x^1, just use x, and instead of x^0, use nothing. Terms where the coefficient is 0 must be omitted.

Example


In this example we multiply the following two sets of polynomials:
x^4+2x^3+x^2+5x and x^3+2x^2+1
x^3+x+1 and 2x^3+x+2

Input

2
4 1 2 1 5 0
3 1 2 0 1

3 1 0 1 1
3 2 0 1 2

Output

x^7 + 4x^6 + 5x^5 + 8x^4 + 12x^3 + x^2 + 5x
2x^6 + 3x^4 + 4x^3 + x^2 + 3x + 2

 


This problem is used for contest: 187  198 


Submit / Problem List / Status / Discuss

 

多项式乘法,就是开两个数组遍历相乘结果存在第三个数组里。

注意输出格式很变态...刚开始写本地运行能过,一提交就超时...

由于最初设想处理“+”时把从第二个起全部写成“ + %dx^%d",于是在for里开了个while找第一项。果断超时。

后来把“+”处理和数字处理分开,AC。

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main()
 5 {
 6     freopen("in.txt", "r", stdin);
 7 
 8     int sample, high1, high2, i, j, k, t;
 9     int a[30], b[30], c[60];
10 
11     scanf("%d", &sample);
12 
13     while (sample--)
14     {
15         memset(c, 0, sizeof(a));
16 
17         scanf("%d", &high1);
18         for (i=high1; i>=0; --i)
19         {
20             scanf("%d", &a[i]);
21         }
22         scanf("%d", &high2);
23         for (i=high2; i>=0; --i)
24         {
25             scanf("%d", &b[i]);
26         }
27 
28         for (i=0; i<high1+1; ++i)
29         {
30             for (j=0; j<high2+1; ++j)
31             {
32                 c[i+j] += a[i]*b[j];
33             }
34         }
35 
36 
37         //处理是0
38         int flagprint = 1;
39         for (k=high1+high2; k>=0; --k)
40         {
41             if (c[k])
42             {
43                 flagprint = 0;
44             }
45         }
46         if (flagprint)
47         {
48             printf("0");
49         }
50 
51         int flag = 0;
52         for (k=high1+high2; k>=0; --k)
53         {
54             if (0 != c[k])
55             {
56                 flag = 1;
57                 if (1 != c[k] && 0 != k)
58                 {
59                     printf("%d", c[k]);
60                 }
61                 else if (0 == k)
62                 {
63                     printf("%d", c[k]);
64                 }
65 
66                 if (k != 0)
67                 {
68                     printf("x");
69                 }
70                 if (k > 1)
71                 {
72                     printf("^%d", k);
73                 }
74             }
75             if (0 != k && flag)
76             {
77                 if (0 != c[k-1])
78                 {
79                     printf(" + ");
80                 }
81 
82             }
83         }
84 
85         printf("\n");
86     }
87 
88     return 0;
89 }

转载于:https://www.cnblogs.com/RootJie/archive/2012/04/17/2454464.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值