(简单) 搜索 HOJ 1105 Egyptian Multiplication
2014-02-08 09:51
495 查看
Egyptian Multiplication
My Tags | (Edit) |
---|
Source : ACM ICPC East Central North America 1994 | |||
Time limit : 3 sec | Memory limit : 32 M |
There is no zero in the number system. There are separate characters denoting ones, tens, hundreds, thousands, ten-thousands, hundred-thousands, millions and ten-millions. For the purposes of this problem, we use near ASCII equivalents for the symbols:| for one (careful, it's a vertical line, not 1)
n for ten
9 for hundred
8 for thousand
r for ten-thousand
(The actual Egyptian hieroglyphs were more picturesque but followed the general shape of these modern symbols. For the purpose of this problem, we will not consider numbers greater than 99,999.)
Numbers were written as a group of ones preceded in turn by groups of tens, hundreds, thousands and ten-thousands. Thus our number 4,023 would be rendered: ||| nn 8888. Notice that a zero digit is indicated by a group consisting of none of the corresponding symbol. The number 40,230 would thus be rendered: nnn 99 rrrr. (In the Rhind Papyrus, the groups are drawn more picturesquely, often spread across more than one horizontal line; but for the purposes of this problem, you should write numbers all on a single line.)
To multiply two numbers a and b, the Egyptians would work with two columns of numbers. They would begin by writing the number | in the left column beside the number a in the right column. They would proceed to form new rows by doubling the numbers in both columns. Notice that doubling can be effected by copying symbols and normalizing by a carrying process if any group of symbols is larger than 9 in size. Doubling would continue as long as the number in the left column does not exceed the other multiplicand b. The numbers in the first column that summed to the multiplicand b were marked with an asterisk. The numbers in the right column alongside the asterisks were then added to produce the result. Below, we show the steps corresponding to the multiplication of 483 by 27:
| * ||| nnnnnnnn 9999 || * |||||| nnnnnn 999999999 |||| || nnn 999999999 8 |||||||| * |||| nnnnnn 99999999 888 |||||| n * |||||||| nn 9999999 8888888 The solution is: | nnnn 888 r(The solution came from adding together:
||| nnnnnnnn 9999 |||||| nnnnnn 999999999 |||| nnnnnn 99999999 888 |||||||| nn 9999999 8888888.)You are to write a program to perform this Egyptian multiplication.Input[/b]Input will consist of several pairs of nonzero numbers written in the Egyptian system described above. There will be one number per line; each number will consist of groups of symbols, and each group is terminated by a single space (including the last group). Input will be terminated by a blank line.Output[/b]For each pair of numbers, your program should print the steps described above used in Egyptian multiplication. Numbers in the left column should be ush with the left margin. Each number in the left and right column will be represented by groups of symbols, and each group is terminated by a single space (including the last group). If there is an asterisk in the left column, it should be separated from the end of the left number by a single space. Up to the 34th character position should then be filled with spaces. Numbers in the right column should begin at the 35th character position on the line and end with a newline character. Test data will be chosen to ensure that no overlap can occur. After showing each of the doubling steps, your program should print the string: "The solution is: " followed by the product of the two numbers in Egyptian notation.Sample Input[/b]||
||
|||
||||
nnnnnn 9
||| n
n
9
|||
8Sample Output[/b]| ||
|| * ||||
The solution is: ||||
| |||
|| ||||||
|||| * || n
The solution is: || n
| * nnnnnn 9
|| nn 999
|||| * nnnn 999999
|||||||| * nnnnnnnn 99 8
The solution is: nnnnnnnn 88
| n
|| nn
|||| * nnnn
|||||||| nnnnnnnn
|||||| n nnnnnn 9
|| nnn * nn 999
|||| nnnnnn * nnnn 999999
The solution is: 8
| |||
|| ||||||
|||| || n
|||||||| * |||| nn
|||||| n |||||||| nnnn
|| nnn * |||||| nnnnnnnnn
|||| nnnnnn * || nnnnnnnnn 9
|||||||| nn 9 * |||| nnnnnnnn 999
|||||| nnnnn 99 * |||||||| nnnnnn 9999999
|| n 99999 * |||||| nnn 99999 8
The solution is: 888
题意:其实就是输入两个数 val,mul 求出val*mul 但是他的求解过程是 对val,1 不断的*2 直到倍数超过mul 就中断,然后从这些数之中选取一些数使得(b1+b2+b3+...+bn)==mul bx 是 (1,2,4,8,16,32,...)等等这些数
思路:选倍数直接dfs就行了,其他就是注意格式输出,基本就没什么问题了
代码:#include<iostream>#include<cstdio>#include<string.h>#include<algorithm>#include<string>#include<deque>#include<queue>#include<math.h>#include<vector>#include<map>#include<stack>#include<set>using namespace std;#define MAX 100000+10#define MOD 99997const int inf = 0xfffffff;char buffer[1000];int left_col[32][10];int right_col[32][10];int sol[10];bool have_star[100];
int read(int &val,int &mul){ val = mul = 0; gets(buffer); if (buffer[0]==0) return 0; int len = strlen(buffer); for (int i = 0 ; i < len ; ++i) { if (buffer[i]=='|') val += 1; else if (buffer[i]=='n') val += 10; else if (buffer[i]=='9') val += 100; else if (buffer[i]=='8') val += 1000; else if (buffer[i]=='r') val += 10000; } gets(buffer); len = strlen(buffer); for (int i = 0 ; i < len ; ++i) { if (buffer[i]=='|') mul += 1; else if (buffer[i]=='n') mul += 10; else if (buffer[i]=='9') mul += 100; else if (buffer[i]=='8') mul += 1000; else if (buffer[i]=='r') mul += 10000; } return 1;}
bool dfs(int k,int rest,int step){ if (rest==0) return true; if (rest-k<0) return false;
have_star[step] = true; if (dfs(2*k,rest-k,step+1)) return true; else { have_star[step] = false; if (dfs(2*k,rest,step+1)) return true; } return false;}
void toString(int k,int* arr){ for (int i = 0 ; i < 5 ; ++i) { arr[i] = k%10; k /= 10; }}
void solution(int val){ toString(val,sol); printf("The solution is: "); for (int j = 0 ; j < sol[0] ; ++j) printf("|"); if (sol[0]) printf(" "); for (int j = 0 ; j < sol[1] ; ++j) printf("n"); if (sol[1]) printf(" "); for (int j = 0 ; j < sol[2] ; ++j) printf("9"); if (sol[2]) printf(" "); for (int j = 0 ;j < sol[3] ; ++j) printf("8"); if (sol[3]) printf(" "); for (int j = 0 ; j < sol[4] ; ++j) printf("r"); if (sol[4]) printf(" "); printf("\n");}
void output(int val,int mul){ int k = 1; int tem; int cnt = 0; while (k<=mul) { toString(k,left_col[cnt]); toString(val*k,right_col[cnt]); ++cnt; k *= 2; } for (int i = 0 ; i < cnt ; ++i) { int tem = 0; for (int j = 0 ; j < left_col[i][0] ; ++j) { tem++; printf("|"); } if (left_col[i][0]) { ++tem; printf(" "); } for (int j = 0 ; j < left_col[i][1] ; ++j) { tem++; printf("n"); } if (left_col[i][1]) { ++tem; printf(" "); }
for (int j = 0 ; j < left_col[i][2] ; ++j) { ++tem; printf("9"); } if (left_col[i][2]) { ++tem; printf(" "); } for (int j = 0 ; j < left_col[i][3] ; ++j) { ++tem; printf("8"); } if (left_col[i][3]) { ++tem; printf(" "); } for (int j = 0 ; j < left_col[i][4] ; ++j) { ++tem; printf("r"); } if (left_col[i][4]) { ++tem; printf(" "); } if (have_star[i]) { ++tem; printf("*"); } for (int j = 0 ; j < 34-tem ; ++j) printf(" "); for (int j = 0 ; j < right_col[i][0] ; ++j) printf("|");
if (right_col[i][0]) printf(" "); for (int j = 0 ; j < right_col[i][1] ; ++j) printf("n"); if (right_col[i][1]) printf(" "); for (int j = 0 ; j < right_col[i][2] ; ++j) printf("9"); if (right_col[i][2]) printf(" "); for (int j = 0 ; j < right_col[i][3] ; ++j) printf("8"); if (right_col[i][3]) printf(" "); for (int j = 0 ; j < right_col[i][4] ; ++j) printf("r"); if (right_col[i][4]) printf(" "); printf("\n"); }}
int main(){ int a,b; while (read(a,b)) { memset(have_star,false,sizeof(have_star)); dfs(1,b,0); output(a,b); solution(a*b); }}
相关文章推荐
- (简单)搜索 HOJ 1022 Spacecraft Malfunction
- poj1111 hoj 1639 Image Perimeters 简单搜索
- (简单)搜索 HOJ 1030 Labyrinth
- Go hdu 4158 hoj 简单搜索
- (简单) 搜索 HOJ 1044 Transportation
- HOJ 1440 Knight Moves -------简单搜索 BFS 求l两点之间最小的到达步数
- (简单) 搜索 HOJ 1048 Cipher
- hdu 4158 hoj 2581 Go 简单搜索
- (简单) 搜索 HOJ 1066 Substrings
- (简单)搜索 HOJ 1070 Word
- (简单)搜索 HOJ 1075 The Castle
- (简单)搜索 HOJ 1692 Choose Your Own Adventure
- (简单) 搜索 HOJ 1085 Finding Rectangles
- (简单) 搜索 HOJ 1097 Robot
- (简单) 搜索 最短路 HOJ 1121 Erdos Numbers
- kuangbin带你飞---简单搜索
- 简单桌面搜索小工具(java)
- poj2488 A Knight's Journey 简单DFS 注意搜索步骤
- 用html或jsp最简单的调用百度搜索的方式
- POJ 1330 Nearest Common Ancestors(LCA 可以用ST算法 NlogN 或者简单搜索)