计算4的1万次方的结果
基本的思想就是模拟人手算的方法,来完成大数运算,很简单.
1
using System;
2
using System.Collections;
3
using System.Diagnostics;
4
5
namespace IBMS.Algorithem
6
{
7
/// <summary>
8
/// N2Class
9
/// </summary>
10
class N2
11
{
12
/// <summary>
13
/// 应用程序的主入口点。
14
/// </summary>
15
[STAThread]
16
static void Main(string[] args)
17
{
18
N2 newObject = new N2();
19
20
int baseNum = 4;
21
int testN = 10000;
22
23
Console.WriteLine("Std Math Lib Caculating result:"+Math.Pow(baseNum,testN).ToString());
24
Console.WriteLine("Wait
now begin to caculate..");
25
26
int[] result = newObject.Power( baseNum,testN );
27
28
Console.WriteLine("Caculate Result:");
29
30
for(int i=result.Length-1; i>=0; i--)
31
{
32
Console.Write(result[i]);
33
}
34
35
Console.WriteLine("");
36
}
37
38
int[] Power(int baseNum, int n)
39
{
40
41
int [] result;
42
43
if(n<0)
44
{
45
46
throw new Exception("现在还不支持负指数");
47
}
48
if(n==0)
49
{
50
result= new int[1];
51
result[0]=1;
52
return result;
53
}
54
55
//获得结果位数
56
int size = (int)(n*Math.Log10(baseNum))+1;
57
58
result = new int[size];
59
result[0]=1;
60
61
for(int i=0; i< n; i++)
62
{
63
Multi_Vector(result,baseNum);
64
//Console.WriteLine("Caculate {0}'s {1} ",baseNum,i+1);
65
}
66
67
return result;
68
}
69
70
void CheckCarray(int []m,int index)
71
{
72
73
if(m[index]>=10)
74
{
75
//向高位进位,模拟手算
76
m[index+1]+=m[index]/10;
77
78
//设置当前位
79
m[index] %= 10;
80
81
//检查下一个位时候需要进位
82
CheckCarray(m,index+1);
83
}
84
}
85
86
87
/// <summary>
88
/// 矢量和一个整数的乘法运算
89
/// </summary>
90
/// <param name="m">矢量</param>
91
/// <param name="n">乘数</param>
92
void Multi_Vector(int [] m, int n)
93
{
94
int totalBit=0;
95
96
for(int i=m.Length-1;i>=0; i--)
97
{
98
if(m[i]>0)
99
{
100
totalBit = i+1; //获得最高位
101
break;
102
}
103
}
104
105
Debug.Assert(totalBit!=0);
106
107
for(int i=totalBit-1; i>=0; i--)
108
{
109
//从高位到低位开始算
110
int temp = m[i]*n;
111
//产生一个进位
112
if(temp>=10)
113
{
114
//向高位进位,模拟手算
115
m[i+1]+=temp/10;
116
//检查该位是否还需要进位
117
CheckCarray(m,i+1);
118
temp %= 10;
119
}
120
m[i]= temp;
121
}
122
}
123
}
124
}
125
using System;2
using System.Collections;3
using System.Diagnostics;4

5
namespace IBMS.Algorithem6
{7
/// <summary>8
/// N2Class9
/// </summary>10
class N211
{12
/// <summary>13
/// 应用程序的主入口点。14
/// </summary>15
[STAThread]16
static void Main(string[] args)17
{18
N2 newObject = new N2();19

20
int baseNum = 4;21
int testN = 10000;22

23
Console.WriteLine("Std Math Lib Caculating result:"+Math.Pow(baseNum,testN).ToString());24
Console.WriteLine("Wait
now begin to caculate..");25

26
int[] result = newObject.Power( baseNum,testN );27
28
Console.WriteLine("Caculate Result:");29

30
for(int i=result.Length-1; i>=0; i--)31
{32
Console.Write(result[i]);33
}34

35
Console.WriteLine("");36
}37

38
int[] Power(int baseNum, int n)39
{40
41
int [] result; 42

43
if(n<0) 44
{45
46
throw new Exception("现在还不支持负指数");47
}48
if(n==0)49
{50
result= new int[1];51
result[0]=1; 52
return result;53
}54

55
//获得结果位数56
int size = (int)(n*Math.Log10(baseNum))+1;57
58
result = new int[size];59
result[0]=1; 60

61
for(int i=0; i< n; i++)62
{63
Multi_Vector(result,baseNum);64
//Console.WriteLine("Caculate {0}'s {1} ",baseNum,i+1);65
}66
67
return result;68
}69

70
void CheckCarray(int []m,int index)71
{72
73
if(m[index]>=10)74
{75
//向高位进位,模拟手算76
m[index+1]+=m[index]/10;77
78
//设置当前位79
m[index] %= 10;80

81
//检查下一个位时候需要进位82
CheckCarray(m,index+1);83
}84
}85

86
87
/// <summary>88
/// 矢量和一个整数的乘法运算89
/// </summary>90
/// <param name="m">矢量</param>91
/// <param name="n">乘数</param>92
void Multi_Vector(int [] m, int n)93
{94
int totalBit=0;95

96
for(int i=m.Length-1;i>=0; i--)97
{98
if(m[i]>0)99
{100
totalBit = i+1; //获得最高位101
break;102
}103
}104
105
Debug.Assert(totalBit!=0);106

107
for(int i=totalBit-1; i>=0; i--)108
{109
//从高位到低位开始算110
int temp = m[i]*n;111
//产生一个进位112
if(temp>=10)113
{ 114
//向高位进位,模拟手算115
m[i+1]+=temp/10;116
//检查该位是否还需要进位117
CheckCarray(m,i+1);118
temp %= 10;119
}120
m[i]= temp;121
}122
}123
}124
}125



浙公网安备 33010602011771号