数组是数据结构中最基本的结构形式,它是一种顺序式的结构,存储的是同一类型的数据。每个数组元素都拥有下标(index)和元素值(value),下标方便存取数据,而元素值就是被存储的数据。
数组使用静态的内存空间配置方式。这也是数组的一个很不方便的地方,在经常需要重新分配数据的存储空间的应用上,往往使用数组就显得非常影响效率;而且,对数组的添加、删除、排序的操作也是比较麻烦以及低效的。
在.net里提供了一种ArrayList的结构,在过去很长一段时间里,我经常会在需要使用集合对象的时候想到它(主要是受早先starter kits的影响),但是ArrayList还是由数组构成的,虽然它在添加元素,删除元素等方面比数组方便了,但是从效率上讲,毕竟它还是基于数组的结构。所谓换汤不换药。
其实,今天我不是想来说数组怎么怎么不好的,而是发挥数组的一些优点,来作一些原本相对复杂的事情,比如,当我们需要计算一个阶乘,而计算结果又超出了我们的数据类型所能存储的范围。
目的:
设计一个可以容纳40位数字的求n!的程序。
思路:
首先,明确我们要解决的问题,在.net的数据结构中,整形数据类型的最大范围是-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807(0 到 18,446,744,073,709,551,615),而当我们计算的这个结果需要有40位,就没有适合的数据结构来存储这个数据。这个时候我们可以借助数组,预先声明一个大小为40的数组,负责存储每一个位数上的整数。
接下来,就是程序设计的思路,聚个例子作为示范,假如我们要计算5!:
第一步:1!
数组内容
数组使用静态的内存空间配置方式。这也是数组的一个很不方便的地方,在经常需要重新分配数据的存储空间的应用上,往往使用数组就显得非常影响效率;而且,对数组的添加、删除、排序的操作也是比较麻烦以及低效的。
在.net里提供了一种ArrayList的结构,在过去很长一段时间里,我经常会在需要使用集合对象的时候想到它(主要是受早先starter kits的影响),但是ArrayList还是由数组构成的,虽然它在添加元素,删除元素等方面比数组方便了,但是从效率上讲,毕竟它还是基于数组的结构。所谓换汤不换药。
其实,今天我不是想来说数组怎么怎么不好的,而是发挥数组的一些优点,来作一些原本相对复杂的事情,比如,当我们需要计算一个阶乘,而计算结果又超出了我们的数据类型所能存储的范围。
目的:
设计一个可以容纳40位数字的求n!的程序。
思路:
首先,明确我们要解决的问题,在.net的数据结构中,整形数据类型的最大范围是-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807(0 到 18,446,744,073,709,551,615),而当我们计算的这个结果需要有40位,就没有适合的数据结构来存储这个数据。这个时候我们可以借助数组,预先声明一个大小为40的数组,负责存储每一个位数上的整数。
接下来,就是程序设计的思路,聚个例子作为示范,假如我们要计算5!:
第一步:1!
数组内容
4 |
3 |
2 |
1 |
0 |
0 |
0 |
1 |
第二步:2!
数组内容
4 |
3 |
2 |
1 |
0 |
0 |
0 |
2*1 |
第三步:3!
数组内容
4 |
3 |
2 |
1 |
0 |
0 |
0 |
2*3 |
第二步:4!
数组内容
4 |
3 |
2 |
1 |
0 |
0 |
0 |
6*4 |
第二步:2!
数组内容
4 |
3 |
2 |
1 |
0 |
0 |
2*5 |
4*5 |
很明显,我们需要做的就是对数组的每一个元素进行累积,超过10以后向前进一位。
程序源码:
1
usingSystem;
2
3
namespaceDsPractice.Array.Factorial
4

{
5
/**////<summary>
6
///利用数组的方式求解指定数字的阶乘。
7
///</summary>
8
classDemo
9

{
10
/**////<summary>
11
///应用程序的主入口点。
12
///</summary>
13
[STAThread]
14
staticvoidMain(string[]args)
15

{
16
DoCalculate();
17
}
18
19
publicstaticvoidDoCalculate()
20

{
21
//选择算法
22
inttype=newNumberReader("Please choose an algorithm: \r\n1. Type A;\r\n2. Type B.",1,2).GetNumber();
23
24
//获取要计算的数字
25
intnumber=newNumberReader("Please input a number to calculate factorial:").GetNumber();
26
27
//获得存放计算结果的数组的长度
28
intlength=newNumberReader("Please input a number of array digit:").GetNumber();
29
30
//创建一个阶乘计算对象
31
Factorialfactorial=newFactorial(number,length);
32
33
//计算并显示结果
34
factorial.ShowResult(type);
35
36
//提示用户继续或结束
37
intres=newNumberReader("Do you wannar try again?\r\n1. Yes;\r\n2. No.",1,2).GetNumber();
38
39
//如果继续执行,则返回重新调用
40
if(res==1)
41

{
42
DoCalculate();
43
}
44
}
45
46
publicclassNumberReader
47

{
48
privateint_min=-999;
49
50
privateint_max=999;
51
52
privatestring_strNumber;
53
54
publicNumberReader(stringtodo)
55

{
56
//提示输入数字
57
Console.WriteLine(todo);
58
//获取数字字符串
59
_strNumber=Console.ReadLine();
60
}
61
62
publicNumberReader(stringtodo,intmin,intmax):this(todo)
63

{
64
this._max=max;
65
this._min=min;
66
}
67
68
publicintGetNumber()
69

{
70
intnumber=0;
71
72
try
73

{
74
number=int.Parse(this._strNumber);
75
76
if(number>this._max||number<this._min)
77

{
78
thrownewException();
79
}
80
}
81
catch(System.FormatExceptionformatEx)
82

{
83
number=newNumberReader("Input format error! Please input again: ").GetNumber();
84
}
85
catch(System.Exceptionex)
86

{
87
number=newNumberReader("Input error! Please input again: ").GetNumber();
88
}
89
90
returnnumber;
91
}
92
}
93
94
publicclassFactorial
95

{
96
//要计算的数字
97
privateint_number=0;
98
99
//结果的位数
100
privateint_digit=1;
101
102
//存放结果的数组
103
privateint[]_data=null;
104
105
//复杂度标记
106
privateint_complex=0;
107
108
publicFactorial(intnumber):this(number,40)
109

{}
110
111
publicFactorial(intnumber,intdigit)
112

{
113
this._number=number;
114
this._data=newint[digit];
115
this._data[0]=1;
116
}
117
118
privatevoidCalculateA()
119

{
120
try
121

{
122
for(inti=1;i<=this._number;i++)
123

{
124
intdigit;
125
for(digit=this._data.GetLength(0);digit>0;digit--)
126

{
127
this._complex++;
128
this._data[digit-1]=this._data[digit-1]*i;
129
130
if(this._data[digit-1]>=10)
131

{
132
for(intj=digit;j<this._data.GetLength(0);j++)
133

{
134
this._complex++;
135
this._data[j]+=this._data[j-1]/

2

3

4



5


6

7

8

9



10


11

12

13

14

15



16

17

18

19

20



21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41



42

43

44

45

46

47



48

49

50

51

52

53

54

55



56

57

58

59

60

61

62

63



64

65

66

67

68

69



70

71

72

73



74

75

76

77



78

79

80

81

82



83

84

85

86



87

88

89

90

91

92

93

94

95



96

97

98

99

100

101

102

103

104

105

106

107

108

109



110

111

112



113

114

115

116

117

118

119



120

121



122

123



124

125

126



127

128

129

130

131



132

133



134

135
