Alan Cai's Blogs
只埋头苦干是不行的,有时候还得抬头看看外面的世界。
 1 using System;
2 using System.Text;
3
4 namespace Labs {
5 public class BigIntegerUtility {
6 public static string Multiply(string one, string other) {
7 if (string.IsNullOrEmpty(one) || string.IsNullOrWhiteSpace(one)) {
8 throw new ArgumentNullException("one");
9 }
10
11 if (string.IsNullOrEmpty(other) || string.IsNullOrWhiteSpace(other)) {
12 throw new ArgumentNullException("other");
13 }
14
15 int oneLength = one.Length;
16 int otherLength = other.Length;
17 int resultLength = oneLength + otherLength;
18 int[] oneNumbers = new int[oneLength];
19 int[] otherNumbers = new int[otherLength];
20 int[] resultNumbers = new int[resultLength];
21
22 for(int i = 0; i < oneLength; i++) {
23 oneNumbers[i] = (int)one[i] - 48;
24 }
25
26 for(int i = 0; i < otherLength; i++) {
27 otherNumbers[i] = (int)other[i] - 48;
28 }
29
30 for(int i = otherLength - 1; i >= 0; i--) {
31 for (int j = oneLength - 1; j >= 0; j--) {
32 resultNumbers[i + j + 1] += otherNumbers[i] * oneNumbers[j];
33 resultNumbers[i + j] += resultNumbers[i + j + 1] / 10;
34 resultNumbers[i + j + 1] = resultNumbers[i + j + 1] % 10;
35 }
36 }
37
38 StringBuilder builder = new StringBuilder();
39
40 for (int i = 0; i < resultLength; i++) {
41 builder.Append(resultNumbers[i]);
42 }
43
44 string result = builder.ToString();
45
46 if (result.StartsWith("0")) {
47 result = result.Substring(1);
48 }
49
50 return result;
51 }
52 }
53 }
posted on 2012-02-06 10:34  Alan Cai  阅读(402)  评论(0编辑  收藏  举报