标签:
Educators are always coming up with new ways to teach math to students. In 2011, an educational software company, All Computer Math (ACM), developed an application to display products in a traditional grade school math format. ACM is now working on an updated version of the software that will display results in a lattice format that some students find to be easier when multiplying larger numbers.
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:
+---------------+ | 3 4 5 | | +---+---+---+ | | |1 /|2 /|2 /| | | | / | / | / |5| |1|/ 5|/ 0|/ 5| | | +---+---+---+ | |/|1 /|2 /|3 /| | | | / | / | / |6| |9|/ 8|/ 4|/ 0| | | +---+---+---+ | |/ 3 / 2 / 0 | +---------------+
The first operand, 345, is displayed above the top of the grid with each digit centered horizontally above its column of the grid, and the second operand, 56, is displayed along the righthand side with each digit centered vertically at the center of its row in the grid. A single cell of the grid, such as
+---+ |3 /| | / | |/ 0| +---+
represents the product of the digit of the first operand that is above its column and the digit of the second operand that is to the right of its row. In our example, this cell represents
the product
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.
To provide an aesthetic view, we use a series of
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
+-----------+ | 1 2 | | +---+---+ | | |0 /|0 /| | | | / | / |2| | |/ 2|/ 4| | | +---+---+ | | |0 /|1 /| | | | / | / |7| |3|/ 7|/ 4| | | +---+---+ | |/ 2 / 4 | +-----------+
Note that in the top-right grid of the lattice, the product 2 * 2 = 04 is displayed with the zero for the tens digit. However, there is no thousands digit displayed in the product 324, nor is there any slash displayed above the digit 3 in that product.
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 | +-------+
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<stdlib.h> using namespace std; int n,m; char str1[101],str2[101]; long long int psum; struct node { int x; int y; } mm[101][101]; int main() { while(scanf("%s%s",str1,str2)!=EOF) { if(strcmp(str1,"0") == 0 && strcmp(str2,"0") == 0) { break; } int flag = 0; long long int p1 = 0,p2 = 0,kk = 1; n = strlen(str1); m = strlen(str2); for(int i=0; i<n; i++) { p1 = p1 * 10 + (str1[i] - '0'); kk = kk *10; } for(int i=0; i<m; i++) { p2 = p2 * 10 + (str2[i] - '0'); kk = kk * 10; } for(int j=0; j<n; j++) { for(int k=0; k<m; k++) { mm[j][k].x = 0; mm[j][k].y = 0; } } kk = kk / 10; psum = p1*p2; for(int j=0; j<m; j++) { for(int k=0; k<n; k++) { int sum = (str1[k]-'0')*(str2[j]-'0'); mm[j][k].x = sum/10; mm[j][k].y = sum%10; } } int pn = (n+1)*3+n; printf("+"); for(int i=0; i<pn; i++) { printf("-"); } printf("+\n"); printf("|"); for(int i=0; i<n; i++) { printf(" %c",str1[i]); } printf(" |\n"); for(int v=0; v<m; v++) { printf("| +"); for(int i=0; i<n; i++) { printf("---+"); } printf(" |\n"); if(v == 0 || flag == 0) { printf("| |"); } else { printf("|/|"); } for(int j=0; j<n; j++) { printf("%d /|",mm[v][j].x); } printf(" |\n"); printf("| |"); for(int j=0; j<n; j++) { printf(" / |"); } printf("%c|\n",str2[v]); printf("|"); if(psum/kk!=0) { printf("%d",(psum/kk)%10); flag = 1; } else if((psum/kk)%10 == 0) { if(flag == 1) { printf("0"); } else { printf(" "); } } printf("|"); for(int j=0; j<n; j++) { printf("/ %d|",mm[v][j].y); } printf(" |\n"); kk = kk/10; } printf("| +"); for(int i=0; i<n; i++) { printf("---+"); } printf(" |\n"); printf("|"); for(int j=0; j<n; j++) { if(j == 0 && flag == 0) { printf(" %d ",(psum/kk)%10); } else { printf("/ %d ",(psum/kk)%10); } kk = kk/10; } printf(" |\n"); printf("+"); for(int i=0; i<pn; i++) { printf("-"); } printf("+\n"); } return 0; }
SDUT 3183 (More) Multiplication(模拟)
标签:
原文地址:http://blog.csdn.net/yeguxin/article/details/45021709