|
|
Posted on
2008-12-10 19:27
eaglet
阅读( 724)
评论()
收藏
举报
using System;
using System.Collections.Generic;
using System.Text;

namespace TestFactorial
  {
 /**//// <summary>
/// 10机制正整数类
/// </summary>
public class DecimalNumber
 {
List<byte> _Data;

public List<byte> Data
 {
get
 {
return _Data;
}
}

public int Length
 {
get
 {
return _Data.Count;
}
}

public DecimalNumber()
 {
_Data = new List<byte>();
}

public DecimalNumber(byte[] data)
 {
foreach (byte b in data)
 {
System.Diagnostics.Debug.Assert(b >= 0 && b <= 9);
}

_Data = new List<byte>(data);
}

public DecimalNumber(List<byte> data)
 {
foreach (byte b in data)
 {
System.Diagnostics.Debug.Assert(b >= 0 && b <= 9);
}

_Data = data;
}

 /**//// <summary>
/// 1位10机制数和10进制序列相乘
///
/// </summary>
/// <param name="s">10进制序列</param>
/// <param name="d">1位10机制数</param>
/// <param name="power">10的幂数</param>
/// <returns></returns>
private static List<byte> Multiply(List<byte> s, byte d, int power)
 {
System.Diagnostics.Debug.Assert(power >= 0);
System.Diagnostics.Debug.Assert(d >= 0 && d <= 9);

List<byte> result = new List<byte>();

for (int i = 0; i < power; i++)
 {
result.Add(0);
}

byte carry = 0; //进位

foreach (byte si in s)
 {
System.Diagnostics.Debug.Assert(si >= 0 && si <= 9);

byte r = (byte)(si* d + carry);
byte m = (byte)(r % 10);
carry = (byte)(r / 10);
result.Add(m);
}

if (carry > 0)
 {
result.Add(carry);
}


return result;
}

 /**//// <summary>
/// 两个10进制序列相加
/// </summary>
/// <param name="s1">序列1</param>
/// <param name="s2">序列2</param>
/// <returns>相加后的序列</returns>
private static List<byte> Plus(List<byte> s1, List<byte> s2)
 {
List<byte> result = new List<byte>();

int c1 = s1.Count;
int c2 = s2.Count;

if (c1 > c2)
 {
for (int i = 0; i < c1 - c2; i++)
 {
s2.Add(0);
}
}
else if (c1 < c2)
 {
for (int i = 0; i < c2 - c1; i++)
 {
s1.Add(0);
}
}

byte carry = 0; //进位

for (int i = 0; i < s1.Count; i++)
 {
System.Diagnostics.Debug.Assert(s1[i] >= 0 && s1[i] <= 9);
System.Diagnostics.Debug.Assert(s2[i] >= 0 && s2[i] <= 9);

byte r = (byte)(s1[i] + s2[i] + carry);
byte m = (byte)(r % 10);
carry = (byte)(r / 10);
result.Add(m);
}

if (carry > 0)
 {
result.Add(carry);
}

return result;
}

public static implicit operator DecimalNumber(string value)
 {
List<byte> data = new List<byte>();

for (int i = value.Length - 1; i >= 0; i--)
 {
data.Add(byte.Parse(value[i].ToString()));
}

return new DecimalNumber(data);
}

public static implicit operator DecimalNumber(int value)
 {
System.Diagnostics.Debug.Assert(value >= 0);
return value.ToString();
}

public static DecimalNumber operator ++(DecimalNumber d)
 {
 return d + new DecimalNumber(new byte[] {1});
}

public static DecimalNumber operator +(DecimalNumber d1, int d2)
 {
System.Diagnostics.Debug.Assert(d2 >= 0);

return d1 + d2.ToString();
}

public static DecimalNumber operator+(DecimalNumber d1, DecimalNumber d2)
 {
return new DecimalNumber(Plus(d1.Data, d2.Data));
}

public static DecimalNumber operator*(DecimalNumber d1, DecimalNumber d2)
 {
List<List<byte>> multiplicationSerial = new List<List<byte>>();

for (int i = 0; i < d1.Data.Count; i++)
 {
multiplicationSerial.Add(Multiply(d2.Data, d1.Data[i], i));
}

List<byte> result = new List<byte>();

foreach(List<byte> s in multiplicationSerial)
 {
result = Plus(s, result);
}
return new DecimalNumber(result);
}

public override string ToString()
 {
StringBuilder str = new StringBuilder();

for (int i = _Data.Count - 1; i >= 0 ; i--)
 {
str.Append(_Data[i].ToString());
}

return str.ToString();
}
}

class Program
 {
static void Main(string[] args)
 {
int d = 1;
DecimalNumber factorial = 1;

while (factorial.Length < 3)
 {
d++;
factorial = factorial * d;
//Console.WriteLine(factorial);
//Console.WriteLine(d);
}

Console.WriteLine(d);
}
}
}

|