An example would be when multiplying 345 * 56 = 19320 as given below, using a lattice grid with 2 rows and 3 columns, which appears inside a surrounding frame:
+---------------+The overall product is then computed by summing along the diagonals in the lattice that represent the same place values in the result. For example, in our first problem the product 19320 was computed as:
1's digit = 0 10's digit = 5 + 3 + 4 = 12, thus 2 with a carry of 1 100's digit = (1 carry) + 2 + 0 + 2 + 8 = 13, thus 3 with a carry of 1 1000's digit = (1 carry) + 2 + 5 + 1 = 9 10000's digit = 1 The resulting product is placed with the one's digit below the grid at the far right and, depending on its length, with the most significant digits wrapped around the left side of the grid. Each digit of the final product appears perfectly aligned with the corresponding diagonal summands.Leading zeros should be displayed within lattice grid cells, but leading zeros should never be displayed in the product, nor should there ever be a slash (/) character prior to the leading digit of the product. For example, consider the product of 12 * 27 = 324 below:
+-----------+
The input contains one or more tests. Each test contains two positive integers, A and B, such that 1 ≤ A ≤ 9999 and 1 ≤ B ≤ 9999. The last data set will be followed by a line containing 0 0.
For each data set, produce the grid that illustrates how to multiply the two numbers using the lattice multiplication technique.
345 56 12 27 1 68 9999 7 3 3 0 0
+---------------+ | 3 4 5 | | +---+---+---+ | | |1 /|2 /|2 /| | | | / | / | / |5| |1|/ 5|/ 0|/ 5| | | +---+---+---+ | |/|1 /|2 /|3 /| | | | / | / | / |6| |9|/ 8|/ 4|/ 0| | | +---+---+---+ | |/ 3 / 2 / 0 | +---------------+ +-----------+ | 1 2 | | +---+---+ | | |0 /|0 /| | | | / | / |2| | |/ 2|/ 4| | | +---+---+ | | |0 /|1 /| | | | / | / |7| |3|/ 7|/ 4| | | +---+---+ | |/ 2 / 4 | +-----------+ +-------+ | 1 | | +---+ | | |0 /| | | | / |6| | |/ 6| | | +---+ | | |0 /| | | | / |8| |6|/ 8| | | +---+ | |/ 8 | +-------+ +-------------------+ | 9 9 9 9 | | +---+---+---+---+ | | |6 /|6 /|6 /|6 /| | | | / | / | / | / |7| |6|/ 3|/ 3|/ 3|/ 3| | | +---+---+---+---+ | |/ 9 / 9 / 9 / 3 | +-------------------+ +-------+ | 3 | | +---+ | | |0 /| | | | / |3| | |/ 9| | | +---+ | | 9 | +-------+
The tables must be formatted precisely as outlined by the rules and examples provided. Mistakes that involve solely errant whitespace will be categorized as Presentation Error; all other errors will be reported as Wrong Answer.
#includeconst int N =100; int main() { int A,B,C,a[N],b[N],c[N],lenA,lenB,lenC,flag; while(scanf("%d%d",&A,&B)>0) { if(A==0&&B==0) break; C=A*B; lenA=lenB=lenC=0; while(A) { a[++lenA]=A%10; A/=10; } while(B) { b[++lenB]=B%10; B/=10; } while(C) { c[++lenC]=C%10; C/=10; } for(int i=1,j=lenA;i<j;i++,j--) {="" a="a[i];a[i]=a[j];a[j]=A;" }="" flag="lenC;" printf("+-");="" for(int="" i="1;i<=lena;i++) d=" ">0;i--) for(int j=1;j<=4;j++) { printf("|"); if(j!=2) { if(j!=4||lenC-lenAi)printf("/"); else printf(" "); } if(j==1) { printf("+"); for(int e=1;e<=lenA;e++) printf("---+"); printf(" |\n"); } else if(j==2) { for(int e=1;e<=lenA;e++) printf("|%d /",(a[e]*b[i])/10); printf("| |\n"); } else if(j==3) { for(int e=1;e<=lenA;e++) printf("| / "); printf("|%d|\n",b[i]); } else { for(int e=1;e<=lenA;e++) printf("|/ %d",(a[e]*b[i])%10); printf("| |\n"); } } printf("| +"); for(int e=1;e<=lenA;e++) printf("---+"); printf(" |\n"); printf("|"); if(flag>lenA) printf("/"); else printf(" "); while(lenC) { printf(" %d ",c[lenC--]); if(lenC) printf("/"); } printf(" |\n"); printf("+-"); for(int i=1;i<=lenA;i++) printf("----"); printf("--+\n"); } } =lena;i++)>
=lena;i++)>