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