10106 - Product

C++语言: Codee#25720
01 /*
02 +++++++++++++++++++++++++++++++++++++++
03                 author: chm
04 +++++++++++++++++++++++++++++++++++++++
05 */
06
07 #include <map>
08 #include <set>
09 #include <list>
10 #include <queue>
11 #include <cmath>
12 #include <stack>
13 #include <bitset>
14 #include <cstdio>
15 #include <cctype>
16 #include <vector>
17 #include <cstdlib>
18 #include <cstring>
19 #include <fstream>
20 #include <sstream>
21 #include <iomanip>
22 #include <iostream>
23 #include <algorithm>
24
25 using namespace std;
26
27 FILE*            fin         = stdin;
28 FILE*            fout         = stdout;
29 const int        max_size     = 10086;
30
31 char stra[max_size], strb[max_size];
32 int numa[max_size], numb[max_size];
33 int ans[max_size];
34
35 void convertnum(char* str, int* num)
36 {
37     int len = strlen(str);
38
39     for(int i = 0, j = len - 1; j >= 0; ++i, --j)
40         num[i] = str[j] - '0';
41 }
42
43 int main()
44 {
45 #ifndef ONLINE_JUDGE
46     freopen("c:\\in.txt", "r", stdin);
47     fout = fopen("c:\\garage\\out.txt", "w");
48 #endif
49     int len1, len2, len;
50     while(scanf("%s\n%s\n", stra, strb) != EOF)
51     {
52         convertnum(stra, numa);
53         convertnum(strb, numb);
54         len1 = strlen(stra);
55         len2 = strlen(strb);
56         memset(ans, 0, sizeof(ans));
57
58         int tmp;
59         for(int i = 0; i < len1; ++i)    //just simulate the procedure of multiply
60             for(int j = 0; j < len2; ++j)
61             {
62                 tmp = numa[i] * numb[j] + ans[i + j];
63                 ans[i + j] = tmp % 10;
64                 ans[i + j + 1] += tmp / 10;
65             }
66         int idx = len1 + len2 + 2;
67         while(idx >= 0 && !ans[idx] )
68             --idx;
69         if(idx < 0 && !ans[0])            // the result is all zero 00000000000,just print '0'
70             fprintf(fout, "0");
71         while(idx >= 0)
72             fprintf(fout, "%d", ans[idx--]);
73         fprintf(fout, "\n");
74     }
75
76 #ifndef ONLINE_JUDGE
77     fclose(fout);
78     system("c:\\garage\\check.exe");
79     system("notepad c:\\garage\\out.txt");
80 #endif
81     return 0;
82 }
83 /*
84 21
85 21
86 42
87 21
88 _____
89 441
90    */
posted @ 2012-03-03 16:31  strorehouse  阅读(246)  评论(0编辑  收藏  举报