CSU1561 (More) Multiplication
来源:互联网 发布:linux 卸载 工具 编辑:程序博客网 时间:2024/06/05 15:33
(More) Multiplication
Description
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 |
+---------------+
|3 /|
| / |
|/ 0|
+---+
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:
To provide an aesthetic view, we use a series of minus (-) characters for horizontal lines, pipe (|) characters for vertical lines, and slash (/) characters for diagonal lines. Furthermore, we use a plus (+) character wherever a horizontal and vertical line meet. Each multiplication lattice is subsequently "boxed" by an outer border. There is a row containing the first operand which is between the topmost border and the top line of the grid, and a row between the bottom of the grid and the bottom border, which contains some portion of the resulting product. There is one column between the leading | and the left edge of the inner grid, which may contain a portion of the resulting product, and one column after the right edge of the inner grid but before the rightmost | border, which contains the second operand. If the product is not long enough to wrap around the bottom-left corner, the column between the left border and the left edge of the grid will containing only spaces. (See the later example of 3 x 3.)
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:
| 1 2 |
| +---+---+ |
| |0 /|0 /| |
| | / | / |2|
| |/ 2|/ 4| |
| +---+---+ |
| |0 /|1 /| |
| | / | / |7|
|3|/ 7|/ 4| |
| +---+---+ |
|/ 2 / 4 |
+-----------+
Input
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.
Output
For each data set, produce the grid that illustrates how to multiply the two numbers using the lattice multiplication technique.
Sample Input
345 5612 271 689999 73 30 0
Sample Output
+---------------+| 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 |+-------+
Hint
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.
——————————————————————————
#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>#include <string>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define LL long longconst LL mod=1e9+7;const int INF=0x3f3f3f3f;#define MAXN 100005struct node{ int a,b;} mp[10][10];int xx[10],yy[10];int ax[10],ay[10];int main(){ int x,y; while(~scanf("%d%d",&x,&y)) { if(x==0&&y==0) break; int t1=0; while(x>0) { xx[t1++]=x%10; x/=10; } for(int i=0; i<t1/2; i++) swap(xx[i],xx[t1-i-1]); int t2=0; while(y>0) { yy[t2++]=y%10; y/=10; } for(int i=0; i<t2/2; i++) swap(yy[i],yy[t2-i-1]); for(int i=0; i<t2; i++) for(int j=0; j<t1; j++) { mp[i][j].a=yy[i]*xx[j]/10; mp[i][j].b=(yy[i]*xx[j])%10; }//计算答案 傻逼了 去模拟 乘一乘就知道了 memset(ax,0,sizeof ax); memset(ay,0,sizeof ay); for(int i=t1-1; i>=0; i--) { int l=t2-1,c=i; int fl=0; while(l>=0&&c<t1) { if(fl==0) { ax[i]+=mp[l][c].b; c++; fl=1; } else { ax[i]+=mp[l][c].a; l--; fl=0; } } while(ax[i]>9) { ax[i]-=10; if(i>0) ax[i-1]+=1; else ay[t2-1]+=1; } } for(int i=t2-1; i>=0; i--) { int l=i,c=0; int fl=0; while(l>=0&&c<t1) { if(fl==0) { ay[i]+=mp[l][c].a; l--; fl=1; } else { ay[i]+=mp[l][c].b; c++; fl=0; } } while(ay[i]>9) { ay[i]-=10; ay[i-1]+=1; } } printf("+--"); for(int i=0; i<t1; i++) printf("----"); printf("-+\n"); printf("| "); for(int i=0; i<t1; i++) printf("%d ",xx[i]); printf("|\n"); printf("| "); for(int i=0; i<t1; i++) printf("+---"); printf("+ |\n"); int fl=0; for(int i=0; i<t2; i++) { if(fl==0) printf("| "); else printf("|/"); for(int j=0; j<t1; j++) printf("|%d /",mp[i][j].a); printf("| |\n"); printf("| "); for(int j=0; j<t1; j++) printf("| / "); printf("|%d|\n",yy[i]); printf("|%c",ay[i]==0&&!fl?' ':'0'+ay[i]); if(ay[i]>0) fl=1; for(int j=0; j<t1; j++) printf("|/ %d",mp[i][j].b); printf("| |\n"); printf("| "); for(int i=0; i<t1; i++) printf("+---"); printf("+ |\n"); } printf("|"); if(fl) printf("/ %c ",ax[0]==0&&!fl?' ':'0'+ax[0]); else printf(" %c ",ax[0]==0&&!fl?' ':'0'+ax[0]); for(int i=1; i<t1; i++) { printf("/ %c ",ax[i]==0&&!fl?' ':'0'+ax[i]); if(ax[i]>0) fl=1; } printf(" |\n"); printf("+--"); for(int i=0; i<t1; i++) printf("----"); printf("-+\n"); } return 0;}
- CSU1561 (More) Multiplication
- CSU1561-(More) Multiplication
- 1561: (More) Multiplication(模似)
- 1561: (More) Multiplication
- CSU 1561-(More) Multiplication
- CSU 1561(More) Multiplication
- SDUT 3183 (More) Multiplication(模拟)
- Multiplication
- more
- more
- more +-*/
- more
- more
- more
- more
- PKU_ACM_3673_Cow Multiplication
- multiplication puzzle
- Multiplication Puzzle
- 推挽输出、开漏输出、线或、线与、竞争、冒险、毛刺【基本概念】
- python2.7安装mysql的数据驱动
- find与逻辑关系运算
- Event事件对象之动画事件和过渡事件
- 如何自己设计一个高效类似于HashMap的容器
- CSU1561 (More) Multiplication
- 算法之二分查找(进阶版 java/c++)
- 工作小结--关联表的(可为空的情况下)条件添加
- 同步的三种方式:volatile、锁、final
- php的数据类型总结
- scala 随笔(8)list 源码解析
- JMock实践---(六)方法调用次数
- Spring Boot浅谈(是什么/能干什么/优点和不足)
- F