1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| #include <stdio.h>
void putX(int A[][2], int numA, int B[][2], int numB); void putSum(int A[][2], int numA, int B[][2], int numB); int main(){ int num1, num2; scanf("%d", &num1); int Arr1[num1][2]; for(int i = 0; i < num1; i++){ scanf("%d", &Arr1[i][0]); scanf("%d", &Arr1[i][1]); } scanf("%d", &num2); int Arr2[num2][2]; for(int i = 0; i < num2; i++){ scanf("%d", &Arr2[i][0]); scanf("%d", &Arr2[i][1]); } putX(Arr1, num1, Arr2, num2); putSum(Arr1, num1, Arr2, num2); return 0; }
void putX(int A[][2], int numA, int B[][2], int numB){ if(numA == 0 || numB == 0){ printf("0 0\n"); return; } int max = A[0][1] + B[0][1] + 1; int Arr[max]; for(int i = 0; i < max; i++){ Arr[i] = 0; } for(int i = 0; i < numA; i++){ for(int k = 0; k < numB; k++){ Arr[A[i][1] + B[k][1]] += A[i][0] * B[k][0]; } } int isNeedSpace = 0; for(int i = max -1; i > -1; i--){ if(Arr[i] == 0) continue; if(isNeedSpace){ printf(" "); } printf("%d %d", Arr[i], i); isNeedSpace = 1; } printf("\n"); return; }
int compote(int a, int b){ if(a == b){ return 0; }else if( a > b){ return 1; }else{ return -1; } }
void putSum(int A[][2], int numA, int B[][2], int numB){ int i = 0, k = 0, isEmpty = 1, isNeedSpace = 0; while(i < numA && k < numB){ int ps = compote(A[i][1], B[k][1]); int sum; switch(ps){ case 0: sum = A[i][0] + B[k][0]; if(sum == 0){ i++; k++; continue; }; if(isNeedSpace) printf(" "); printf("%d %d", sum, A[i][1]); isNeedSpace = 1; isEmpty = 0; i++; k++; break; case 1: if(isNeedSpace) printf(" "); printf("%d %d", A[i][0], A[i][1]); isNeedSpace = 1; isEmpty = 0; i++; break; case -1: if(isNeedSpace) printf(" "); printf("%d %d", B[k][0], B[k][1]); isNeedSpace = 1; isEmpty = 0; k++; break; } } if(i < numA){ for(;i < numA;i++){ if(isNeedSpace){ printf(" "); } printf("%d %d", A[i][0], A[i][1]); isEmpty = 0; isNeedSpace = 1; } }else if(k < numB){ for(;k < numB;k++){ if(isNeedSpace){ printf(" "); } printf("%d %d", B[k][0], B[k][1]); isEmpty = 0; isNeedSpace = 1; } } if(isEmpty){ printf("0 0"); } return; }
|