fork(1) download
  1. //multiplication of nth order polynomials
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. void printpolynomial(int a[], int n)
  6. {
  7. int i;
  8. for(i=n; i>=0; i--)
  9. printf("%d ", a[i]);
  10. printf("\n");
  11. }
  12.  
  13. void polymult()
  14. {
  15. int i, n;
  16. scanf("%d", &n);
  17.  
  18. int *a= (int *) malloc((n+1)*sizeof(int));
  19. for(i=n; i>=0; i--)
  20. scanf("%d", &a[i]);
  21.  
  22. int *b= (int *) malloc((n+1)*sizeof(int));
  23. for(i=n; i>=0; i--)
  24. scanf("%d", &b[i]);
  25.  
  26. int *answer= (int *) malloc((2*n+1)*sizeof(int));
  27. for(i=0; i<(2*n+1); i++)
  28. answer[i]=0;
  29.  
  30. int index_a, index_b;
  31.  
  32. for(index_a=0; index_a<=n; index_a++)
  33. for(index_b=0; index_b<=n; index_b++)
  34. answer[index_a+index_b]+=(a[index_a]*b[index_b]);
  35.  
  36. printpolynomial(answer, 2*n);
  37. free(a); free(b); free(answer);
  38. }
  39.  
  40. void main()
  41. {
  42. int t;
  43. scanf("%d", &t);
  44. while(t-->0)
  45. polymult();
  46. }
Success #stdin #stdout 0s 2188KB
stdin
2
2
1 2 3
3 2 1
2
1 0 1
2 1 0
stdout
3 8 14 8 3 
2 1 2 1 0