从浅到深
1、
2、百钱百鸡问题。“鸡翁一,值钱五,鸡母一,值钱三,鸡雏三,值钱一,百钱买百鸡,问翁、母、雏各几何?”
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
百钱百鸡
1
int cocks, hens, chicks, count=0; //依次是公鸡数,母鸡数,小鸡数,以及符合条件的结果数
2
for(cocks=0;cocks<=20;cocks++)
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
4
for (hens = 0; hens <= 33; hens++)
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
6
for (chicks = 0; chicks <= 100; chicks=chicks+3)
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
if (5 * cocks + 3 * hens + chicks / 3 == 100 && cocks + hens + chicks == 100)
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
Console.WriteLine("{0}只公鸡,{1}只母鸡,{2}只小鸡", cocks, hens, chicks);
11
count++;
12
}
13
}
14
}
15
}
16
Console.WriteLine("共以上{0}种符合百钱买百鸡的要求",count);
17
Console.ReadLine();
3、高精度计算
32位的int,64位的long撑爆了怎么办?
考虑高精度的计算?
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
BigInt num1 = new BigInt("12345678901");
2
BigInt num2 = new BigInt("23456789012");
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
Console.WriteLine("{0} {1} ", num1, num2);
5
Console.WriteLine("12345678901+23456789012={0}", num1 + num2);
6
Console.WriteLine("12345678901+23456789012={0}", (long)12345678901 + (long)23456789012);
7
Console.WriteLine("12345678901-23456789012={0}", num1 - num2);
8
Console.WriteLine("12345678901-23456789012={0}", (long)12345678901 - (long)23456789012);
9
Console.WriteLine("12345678901*23456789012={0}", num1 * num2);
10
// Console.WriteLine("12345678901*23456789012={0}", (long)12345678901 * (long)23456789012);
实际上在算(long)12345678901 * (long)23456789012时已经撑爆了
那么,定义一个BigInt,用来做高精度计算,大数据不再烦恼
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
高精度数字类
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
2
/// 高精度数字类
3
/// </summary>
4
public class BigInt
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
6
private int[] _value;
7
private int _length;
8
private bool _negative = false;
9
public int[] Value
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _value; }
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _value = value; }
13
}
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
15
/// 是否是负数
16
/// </summary>
17
public bool Negative
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _negative; }
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _negative = value; }
21
}
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
24
/// 由包含数字的字符串创建对象
25
/// </summary>
26
/// <param name="strValue">包含数字的字符串</param>
27
public BigInt(string strValue)
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
_length = strValue.Length;
30
_value = new int[_length];
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
// 将字符串形式的数字逆序排放
33
for (int i = 0; i < _length; i++)
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35
_value[i] = strValue[_length - i - 1] - '0';
36
}
37
}
38
public BigInt(string strValue, bool negative)
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
_length = strValue.Length;
41
this.Negative = negative;
42
_value = new int[_length];
43![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
// 将字符串形式的数字逆序排放
45
for (int i = 0; i < _length; i++)
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47
_value[i] = strValue[_length - i - 1] - '0';
48
}
49
}
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
52
/// 由逆序数组创建对象
53
/// </summary>
54
/// <param name="vectorValue">逆序存放数字的数组</param>
55
public BigInt(int[] vectorValue)
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
_value = vectorValue;
58
_length = vectorValue.Length;
59
}
60
public BigInt(int[] vectorValue, bool negative)
61![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
62
_value = vectorValue;
63
_length = vectorValue.Length;
64
this.Negative = negative;
65
}
66![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
67
/// 转换为字符串形式的结果
68
/// </summary>
69
/// <returns></returns>
70
public override string ToString()
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
72
bool isPadZero = true; // 标志前导零
73
StringBuilder sb = new StringBuilder();
74
//负号
75
if (this.Negative)
76
sb.Append("-");
77
// 逆序输出
78
for (int i = _length - 1; i >= 0; i--)
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
// 如果当前有前导零,那么就不输出数字0,而遇到非零值的时候
81
// 不但要输出,还要将目前的前导零状态进行修改
82
if (isPadZero)
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
84
if (_value[i] == 0)
85![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86
continue;
87
}
88
else
89![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
90
isPadZero = false;
91
sb.Append(_value[i].ToString());
92
}
93
}
94
else
95![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
96
sb.Append(_value[i].ToString());
97
}
98
}
99![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
100
return sb.ToString();
101
}
102![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
103
/// 减法
104
/// </summary>
105
/// <param name="num1"></param>
106
/// <param name="num2"></param>
107
/// <returns></returns>
108
public static BigInt operator -(BigInt num1, BigInt num2)
109![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
110
bool negative = false;
111
int[] value1;
112
int[] value2;
113
if (num1.IsBigThan(num2))
114![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
115
value1 = num1.Value;
116
value2 = num2.Value;
117
}
118
else
119![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
120
value1 = num2.Value;
121
value2 = num1.Value;
122
negative = true;
123
}
124
// 初始条件
125
int maxLength = value1.Length;
126
int minLength = value2.Length;
127![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
128
int[] result = new int[maxLength];
129![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
130
// 按照最小的那个数的位数进行相减
131
for (int i = 0; i < minLength; i++)
132![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
133
result[i] = value1[i] - value2[i];
134
}
135![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
136
// 将较长的数的剩余部分补足
137
int[] lagerValue = value1.Length > value2.Length ? value1 : value2;
138
for (int i = minLength; i < maxLength; i++)
139![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
140
result[i] = lagerValue[i];
141
}
142![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
143
// 进位
144
for (int i = 0; i < maxLength; i++)
145![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
146
if (result[i] <0)
147![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
148
result[i + 1] -= 1;
149
result[i] += 10;
150
}
151
}
152![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
153
// 构造结果并返回
154
BigInt resultInt = new BigInt(result, negative);
155
return resultInt;
156![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
157
}
158![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
159
/// 乘法
160
/// </summary>
161
/// <param name="num1"></param>
162
/// <param name="num2"></param>
163
/// <returns></returns>
164
public static BigInt operator *(BigInt num1, BigInt num2)
165![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
166
// 初始条件
167
int[] value1 = num1.IsBigThan(num2) ? num1.Value : num2.Value;//大数
168
int[] value2 = num1.IsBigThan(num2) ? num2.Value : num1.Value;//小数
169![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
170
int maxLength = value1.Length;
171
int minLength = value2.Length;
172![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
173
List<BigInt> result = new List<BigInt>();
174
int[] temp = new int[maxLength + minLength];
175![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
176
for (int i = 0; i < minLength; i++)
177![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
178
for (int j = 0; j < maxLength; j++)
179![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
180
temp[j] = value2[i] * value1[j];
181
}
182
for (int j = 0; j < maxLength; j++)
183![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
184
if (temp[j] >= 10)
185![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
186
temp[i + 1] += temp[i] / 10; // 10的倍数进位
187
temp[i] %= 10; // 对10的余数留下
188
}
189
}
190
BigInt tempInt=new BigInt(temp);
191
string tempString=tempInt.ToString();
192
//补零
193
for(int k=0;k<i;k++)
194![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
195
tempString+="0";
196
}
197
result.Add(new BigInt(tempString));
198
}
199![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
200
BigInt resultInt = new BigInt("0");
201
foreach(BigInt xx in result)
202![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
203
resultInt += xx;
204
}
205
return resultInt;
206![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
207
}
208![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
209
/// 重载加法运算
210
/// </summary>
211
/// <param name="num1">加数1</param>
212
/// <param name="num2">加数2</param>
213
/// <returns></returns>
214
public static BigInt operator +(BigInt num1, BigInt num2)
215![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
216
// 初始条件
217
int[] value1 = num1.Value;
218
int[] value2 = num2.Value;
219![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
220
int maxLength = Math.Max(value1.Length, value2.Length);
221
int minLength = Math.Min(value1.Length, value2.Length);
222
223
int[] result = new int[maxLength + 1];
224![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
225
// 按照最小的那个数的位数进行相加
226
for (int i = 0; i < minLength; i++)
227![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
228
result[i] = value1[i] + value2[i];
229
}
230![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
231
// 将较长的数的剩余部分补足
232
int[] lagerValue = value1.Length > value2.Length ? value1 : value2;
233
for (int i = minLength; i < maxLength; i++)
234![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
235
result[i] = lagerValue[i];
236
}
237![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
238
// 进位
239
for (int i = 0; i < maxLength; i++)
240![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
241
if (result[i] >= 10)
242![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
243
result[i + 1] += result[i] / 10; // 10的倍数进位
244
result[i] %= 10; // 对10的余数留下
245
}
246
}
247![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
248
// 构造结果并返回
249
BigInt resultInt = new BigInt(result);
250
return resultInt;
251
}
252
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
IsBigThan
1
public static class Helper
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
public static bool IsBigThan(this BigInt num1, BigInt num2)
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
if (num1.Value.Length > num2.Value.Length)
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7
return true;
8
}
9
else if (num1.Value.Length == num2.Value.Length)
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
for (int i = num1.Value.Length - 1; i >= 0; i--)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
if (num1.Value[i] > num2.Value[i])
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
return true;
16
}
17
else if (num1.Value[i] == num2.Value[i])
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
continue;
20
}
21
else
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
return false;
24
}
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
}
27
return false;
28
29
}
30
else
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
return false;
33
}
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
}
37
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
斐波那契数列
var f1 = 1d;
var f2 = 1d;
for (var i = 0; i < 100; i++)
{
if (i == 0 || i == 1)
{
Console.Write("1 ");
}
else
{
var f = f1 + f2;
f1 = f2;
f2 = f;
Console.Write("{0} ", f);
if (i % 9 == 0)
{
Console.WriteLine();
}
}
}
Console.ReadLine();