求一个C#阶乘的算法(能算100以上的),用字符串来保存结果。在线等待。谢谢答复。
解决方案 »
- C#通信服务数据丢失
- 水晶报表动态绑定公式字段
- 高手,大拿 ,大牛 ,展现你们的时候到了 ,小弟遇到问题 ,恳求各位帮忙啊 。眼泪啊 ,先谢谢各位了 ,不会的也可以来捧场
- 另类声音识别
- 请教一个继承组件时会会复加载属性的问题。
- 如何读取tif(BMP、JPG)文件的条形码?在线等。thanks!
- 如何转码,鬼码
- 请问在C#中如果可以得到当前窗体的尺寸?
- OpenFileDialog.Filter可以设置选择文件的文件名前几个字为固定值么?
- 一觉醒来又多了条底裤-----散分100
- 对于类中的只读且需要动态生成的成员一般用属性的get访问器还是用无参数的函数
- 【问个小问题】....顺便庆祝升星.............标题要长
http://topic.csdn.net/u/20080520/16/c6968aa3-be5f-4592-85bc-b8739d698530.html
用数组存储
public static uint[] CalculateLargeNumber(int n)
{
if (n < 0) { throw new ArgumentOutOfRangeException("n必须为非负数。"); } if (n == 0 || n == 1) { return new uint[] { 1 }; }
// 数组的最大长度
const int MaxLength = 100000;
uint[] array = new uint[MaxLength];
// 1! = 1
array[0] = 1;
int i = 0;
int j = 0;
// valid为当前阶乘值的位数(如5! = 120,此时valid = 3)
int valid = 1; for (i = 2; i <= n; i++)
{
long carry = 0;
for (j = 0; j < valid; j++)
{
long multipleResult = array[j] * i + carry;
// 计算当前位的数值
array[j] = (uint)(multipleResult % 10);
// 计算进到高位的数值
carry = multipleResult / 10;
}
// 为更高位赋值
while (carry != 0)
{
array[valid++] = (uint)(carry % 10);
carry /= 10;
}
}
// 截取有效元素
uint[] result = new uint[valid];
Array.Copy(array, result, valid);
return result;
}
static void Main(string[] args)
{
uint[] arr = CalculateLargeNumber(100);
string str = "";
for (int i = arr.Length-1; i >=0 ; i--)
{
str += arr[i].ToString();
}
Console.WriteLine(str);
Console.ReadKey();
}给你链接,不好好看
using System.Text;
using System.Collections.Generic;namespace Skyiv
{
class Factorial
{
const int n = 100;
static void Main()
{
BigInteger f = 1;
for (int i = 2; i <= n; i++)
{
f *= i;
}
Console.WriteLine("{0}! = {1}", n, f);
}
}
static class Utility
{
public static void Swap<T>(ref T x, ref T y)
{
T z = x;
x = y;
y = z;
}
} sealed class BigInteger : IEquatable<BigInteger>, IComparable<BigInteger>
{
static readonly int Len = 9; // int.MaxValue = 2,147,483,647
static readonly int Base = (int)Math.Pow(10, Len); int sign;
List<int> data; BigInteger(long x)
{
sign = (x == 0) ? 0 : ((x > 0) ? 1 : -1);
data = new List<int>();
for (ulong z = (x < 0) ? (ulong)-x : (ulong)x; z != 0; z /= (ulong)Base) data.Add((int)(z % (ulong)Base));
} BigInteger(BigInteger x)
{
sign = x.sign; // x != null
data = new List<int>(x.data);
} public static implicit operator BigInteger(long x)
{
return new BigInteger(x);
} public static BigInteger Parse(string s)
{
if (s == null) return null;
s = s.Trim().Replace(",", null);
if (s.Length == 0) return 0;
BigInteger z = 0;
z.sign = (s[0] == '-') ? -1 : 1;
if (s[0] == '-' || s[0] == '+') s = s.Substring(1);
int r = s.Length % Len;
z.data = new List<int>(new int[s.Length / Len + ((r != 0) ? 1 : 0)]);
int i = z.data.Count - 1;
if (r != 0) z.data[i--] = int.Parse(s.Substring(0, r));
for (; i >= 0; i--, r += Len) z.data[i] = int.Parse(s.Substring(r, Len));
z.Shrink();
return z;
} public static BigInteger Abs(BigInteger x)
{
if (x == null) return null;
BigInteger z = new BigInteger(x);
z.sign = Math.Abs(x.sign);
return z;
} public static BigInteger Pow(BigInteger x, long y)
{
if (x == null) return null;
BigInteger z = 1, n = x;
for (; y > 0; y >>= 1, n *= n) if ((y & 1) != 0) z *= n;
return z;
} public static BigInteger operator +(BigInteger x)
{
if (x == null) return null;
return new BigInteger(x);
} public static BigInteger operator -(BigInteger x)
{
if (x == null) return null;
BigInteger z = new BigInteger(x);
z.sign = -x.sign;
return z;
} public static BigInteger operator ++(BigInteger x)
{
return x + 1;
} public static BigInteger operator --(BigInteger x)
{
return x - 1;
} public static BigInteger operator +(BigInteger x, BigInteger y)
{
if (x == null || y == null) return null;
if (x.AbsCompareTo(y) < 0) Utility.Swap(ref x, ref y);
BigInteger z = new BigInteger(x);
if (x.sign * y.sign == -1) z.AbsSubtract(y);
else z.AbsAdd(y);
return z;
} public static BigInteger operator -(BigInteger x, BigInteger y)
{
if (x == null || y == null) return null;
return x + (-y);
} public static BigInteger operator *(BigInteger x, BigInteger y)
{
if (x == null || y == null) return null;
BigInteger z = 0;
z.sign = x.sign * y.sign;
z.data = new List<int>(new int[x.data.Count + y.data.Count]);
for (int i = x.data.Count - 1; i >= 0; i--)
for (int j = y.data.Count - 1; j >= 0; j--)
{
long n = Math.BigMul(x.data[i], y.data[j]);
z.data[i + j] += (int)(n % Base);
z.CarryUp(i + j);
z.data[i + j + 1] += (int)(n / Base);
z.CarryUp(i + j + 1);
}
z.Shrink();
return z;
} public static BigInteger operator /(BigInteger dividend, BigInteger divisor)
{
BigInteger remainder;
return DivRem(dividend, divisor, out remainder);
} public static BigInteger operator %(BigInteger dividend, BigInteger divisor)
{
BigInteger remainder;
DivRem(dividend, divisor, out remainder);
return remainder;
} public static BigInteger DivRem(BigInteger dividend, BigInteger divisor, out BigInteger remainder)
{
remainder = null;
if (dividend == null || divisor == null) return null;
if (divisor.sign == 0) throw new DivideByZeroException();
if (dividend.AbsCompareTo(divisor) < 0)
{
remainder = new BigInteger(dividend);
return 0;
}
remainder = 0;
BigInteger quotient = 0;
quotient.sign = dividend.sign * divisor.sign;
quotient.data = new List<int>(new int[dividend.data.Count]);
divisor = Abs(divisor); // NOT: divisor.sign = Math.Abs(divisor.sign);
for (int i = dividend.data.Count - 1; i >= 0; i--)
{
remainder = remainder * Base + dividend.data[i];
int iQuotient = remainder.BinarySearch(divisor, -1);
quotient.data[i] = iQuotient;
remainder -= divisor * iQuotient;
}
quotient.Shrink();
if (remainder.sign != 0) remainder.sign = dividend.sign;
return quotient;
} public static BigInteger Sqrt(BigInteger x)
{
if (x == null || x.sign < 0) return null;
BigInteger root = 0;
root.sign = 1;
root.data = new List<int>(new int[x.data.Count / 2 + 1]);
for (int i = root.data.Count - 1; i >= 0; i--) root.data[i] = x.BinarySearch(root, i);
root.Shrink();
return root;
} int BinarySearch(BigInteger divisor, int i)
{
int low = 0, high = Base - 1, mid = 0, cmp = 0;
while (low <= high)
{
mid = (low + high) / 2;
cmp = CompareTo(divisor, mid, i);
if (cmp > 0) low = mid + 1;
else if (cmp < 0) high = mid - 1;
else return mid;
}
return (cmp < 0) ? (mid - 1) : mid;
} int CompareTo(BigInteger divisor, int mid, int i)
{
if (i >= 0) divisor.data[i] = mid;
return AbsCompareTo(divisor * ((i >= 0) ? divisor : mid));
} void AbsAdd(BigInteger x)
{
for (int i = 0; i < x.data.Count; i++)
{
data[i] += x.data[i];
CarryUp(i);
}
} void AbsSubtract(BigInteger x)
{
for (int i = 0; i < x.data.Count; i++)
{
data[i] -= x.data[i];
CarryDown(i);
}
Shrink();
} void CarryUp(int n)
{
for (; data[n] >= Base; n++)
{
if (n == data.Count - 1) data.Add(data[n] / Base);
else data[n + 1] += data[n] / Base;
data[n] %= Base;
}
} void CarryDown(int n)
{
for (; data[n] < 0; n++)
{
data[n + 1]--;
data[n] += Base;
}
Shrink();
} void Shrink()
{
for (int i = data.Count - 1; i >= 0 && data[i] == 0; i--) data.RemoveAt(i);
if (data.Count == 0) sign = 0;
} public static bool operator ==(BigInteger x, BigInteger y)
{
if (object.ReferenceEquals(x, null)) return object.ReferenceEquals(y, null);
return x.Equals(y);
} public static bool operator !=(BigInteger x, BigInteger y)
{
if (object.ReferenceEquals(x, null)) return !object.ReferenceEquals(y, null);
return !x.Equals(y);
} public static bool operator <(BigInteger x, BigInteger y)
{
if (object.ReferenceEquals(x, null)) return !object.ReferenceEquals(y, null);
return x.CompareTo(y) < 0;
} public static bool operator >(BigInteger x, BigInteger y)
{
if (object.ReferenceEquals(x, null)) return false;
return x.CompareTo(y) > 0;
} public static bool operator <=(BigInteger x, BigInteger y)
{
if (object.ReferenceEquals(x, null)) return true;
return x.CompareTo(y) <= 0;
} public static bool operator >=(BigInteger x, BigInteger y)
{
if (object.ReferenceEquals(x, null)) return object.ReferenceEquals(y, null);
return x.CompareTo(y) >= 0;
} public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (sign < 0) sb.Append('-');
sb.Append((data.Count == 0) ? 0 : data[data.Count - 1]);
for (int i = data.Count - 2; i >= 0; i--) sb.Append(data[i].ToString("D" + Len));
return sb.ToString();
} public override int GetHashCode()
{
int hash = sign;
foreach (int n in data) hash ^= n;
return hash;
} public override bool Equals(object other)
{
if (other == null || GetType() != other.GetType()) return false;
return Equals(other as BigInteger);
} public bool Equals(BigInteger other)
{
return CompareTo(other) == 0;
} public int CompareTo(BigInteger other)
{
if (object.ReferenceEquals(other, null)) return 1;
if (sign < other.sign) return -1;
if (sign > other.sign) return 1;
if (sign == 0) return 0;
return sign * AbsCompareTo(other);
} int AbsCompareTo(BigInteger other)
{
if (data.Count < other.data.Count) return -1;
if (data.Count > other.data.Count) return 1;
for (int i = data.Count - 1; i >= 0; i--)
if (data[i] != other.data[i])
return (data[i] < other.data[i]) ? -1 : 1;
return 0;
}
}
}
using System.Text;
using System.Collections.Generic;namespace Skyiv
{
class Factorial
{
const int n = 100;
static void Main()
{
BigInteger f = 1;
for (int i = 2; i <= n; i++)
{
f *= i;
}
Console.WriteLine("{0}! = {1}", n, f);
}
} sealed class BigInteger
{
static readonly int Len = 9; // int.MaxValue = 2,147,483,647
static readonly int Base = (int)Math.Pow(10, Len); int sign;
List<int> data; BigInteger(long x)
{
sign = (x == 0) ? 0 : ((x > 0) ? 1 : -1);
data = new List<int>();
for (ulong z = (x < 0) ? (ulong)-x : (ulong)x; z != 0; z /= (ulong)Base) data.Add((int)(z % (ulong)Base));
} BigInteger(BigInteger x)
{
sign = x.sign; // x != null
data = new List<int>(x.data);
} public static implicit operator BigInteger(long x)
{
return new BigInteger(x);
} public static BigInteger operator +(BigInteger x, BigInteger y)
{
if (x == null || y == null) return null;
if (x.AbsCompareTo(y) < 0) Swap(ref x, ref y);
BigInteger z = new BigInteger(x);
if (x.sign * y.sign == -1) z.AbsSubtract(y);
else z.AbsAdd(y);
return z;
} public static BigInteger operator *(BigInteger x, BigInteger y)
{
if (x == null || y == null) return null;
BigInteger z = 0;
z.sign = x.sign * y.sign;
z.data = new List<int>(new int[x.data.Count + y.data.Count]);
for (int i = x.data.Count - 1; i >= 0; i--)
for (int j = y.data.Count - 1; j >= 0; j--)
{
long n = Math.BigMul(x.data[i], y.data[j]);
z.data[i + j] += (int)(n % Base);
z.CarryUp(i + j);
z.data[i + j + 1] += (int)(n / Base);
z.CarryUp(i + j + 1);
}
z.Shrink();
return z;
} void AbsAdd(BigInteger x)
{
for (int i = 0; i < x.data.Count; i++)
{
data[i] += x.data[i];
CarryUp(i);
}
} void AbsSubtract(BigInteger x)
{
for (int i = 0; i < x.data.Count; i++)
{
data[i] -= x.data[i];
CarryDown(i);
}
Shrink();
} void CarryUp(int n)
{
for (; data[n] >= Base; n++)
{
if (n == data.Count - 1) data.Add(data[n] / Base);
else data[n + 1] += data[n] / Base;
data[n] %= Base;
}
} void CarryDown(int n)
{
for (; data[n] < 0; n++)
{
data[n + 1]--;
data[n] += Base;
}
Shrink();
} void Shrink()
{
for (int i = data.Count - 1; i >= 0 && data[i] == 0; i--) data.RemoveAt(i);
if (data.Count == 0) sign = 0;
} public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (sign < 0) sb.Append('-');
sb.Append((data.Count == 0) ? 0 : data[data.Count - 1]);
for (int i = data.Count - 2; i >= 0; i--) sb.Append(data[i].ToString("D" + Len));
return sb.ToString();
} int AbsCompareTo(BigInteger other)
{
if (data.Count < other.data.Count) return -1;
if (data.Count > other.data.Count) return 1;
for (int i = data.Count - 1; i >= 0; i--)
if (data[i] != other.data[i])
return (data[i] < other.data[i]) ? -1 : 1;
return 0;
} public static void Swap<T>(ref T x, ref T y)
{
T z = x;
x = y;
y = z;
}
}
}
http://www.cnblogs.com/skyivben/archive/2008/07/13/1241681.html
Type t = Type.GetType("System.Numeric.BigInteger, System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
DynamicMethod dm = new DynamicMethod(string.Empty, typeof(string), new Type[] { typeof(int) }, true);
var il = dm.GetILGenerator();
var current = il.DeclareLocal(t);
var factor = il.DeclareLocal(typeof(int));
var exit = il.DefineLabel();
var condition = il.DefineLabel();
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Stloc_S, factor);
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Newobj, t.GetConstructor(new Type[] { typeof(int) }));
il.Emit(OpCodes.Stloc_S, current);
il.Emit(OpCodes.Br_S, condition);
il.MarkLabel(condition);
il.Emit(OpCodes.Ldloc_S, factor);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Bgt_S, exit);
il.Emit(OpCodes.Ldloc_S, factor);
il.Emit(OpCodes.Newobj, t.GetConstructor(new Type[] { typeof(int) }));
il.Emit(OpCodes.Ldloc_S, current);
il.Emit(OpCodes.Call, t.GetMethod("Multiply"));
il.Emit(OpCodes.Stloc_S, current);
il.Emit(OpCodes.Ldloc_S, factor);
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Stloc_S, factor);
il.Emit(OpCodes.Br_S, condition);
il.MarkLabel(exit);
il.Emit(OpCodes.Ldloca_S, current);
il.Emit(OpCodes.Call, t.GetMethod("ToString", Type.EmptyTypes));
il.Emit(OpCodes.Ret);
var fac = (Func<int, string>)dm.CreateDelegate(typeof(Func<int, string>));
//计算100的阶乘
Console.WriteLine(fac(100));
class Program
{
public double Factorial(int i)
{
if (i < 1)
{
i = 1;
return i;
}
return i * Factorial(i - 1);
}
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine(p.Factorial(100).ToString ());
}就用递归就好了,干嘛考虑那么复杂
{
if (n < 0) { throw new ArgumentOutOfRangeException("n必须为非负数。"); } if (n == 0 || n == 1) { return new uint[] { 1 }; }
// 数组的最大长度
const int MaxLength = 100000;
uint[] array = new uint[MaxLength];
// 1! = 1
array[0] = 1;
int i = 0;
int j = 0;
// valid为当前阶乘值的位数(如5! = 120,此时valid = 3)
int valid = 1; for (i = 2; i <= n; i++)
{
long carry = 0;
for (j = 0; j < valid; j++)
{
long multipleResult = array[j] * i + carry;
// 计算当前位的数值
array[j] = (uint)(multipleResult % 10);
// 计算进到高位的数值
carry = multipleResult / 10;
}
// 为更高位赋值
while (carry != 0)
{
array[valid++] = (uint)(carry % 10);
carry /= 10;
}
}
// 截取有效元素
uint[] result = new uint[valid];
Array.Copy(array, result, valid);
return result;
}
static void Main(string[] args)
{
uint[] arr = CalculateLargeNumber(100);
string str = "";
for (int i = arr.Length-1; i >=0 ; i--)
{
str += arr[i].ToString();
}
Console.WriteLine(str);
Console.ReadKey();
}
O(∩_∩)O哈哈~
计算 10000! 以内的所有阶乘,用字符串保存结果,我在此博文有引述
{
ulong uVal=1;
if(para>1)
{
uVal=para* Factorial(para-1);
}
return uVal;
}剩下的就是调用的事了,在输入结果时用.ToString()输出就行了。
楼上的各位仁兄搞那么复杂,窃以为没有必要。
#include <iostream>
using namespace std;
void main()
{
int Data[40];
int Digit;
int i,j,r,k;
int N;
for(i=1;i <40-1;i++)
Data[i] = 0;
Data[0]=1;
Data[1]=1;
Digit = 1;
printf("Enter a number what you want to calculus: ");
scanf("%d",&N);
for(i=1;i <N+1;i++)
{
for(j=1;j <Digit+1;j++)
Data[j] *= i;
for(j=1;j <Digit+1;j++)
{
if(Data[j]>10)
{
for(r=1;r <Digit+1;r++)
{
if(Data[Digit]>10)
Digit++;
Data[r+1] += Data[r]/10;
Data[r] = Data[r]%10;
}
}
}
printf("%d! = ",i);
for(k=Digit;k>0;k--)
printf("%d",Data[k]);
printf("\n");
}
system("pause");
}
#include<stdlib.h>
#define N 100
void main()
{
int a[N] = { 1 }, i, c, m, n;
for( m = 0, n = N ; n > 1; n-- )
{
for( i = c = 0; i <= m; i++ )
{
a[i] = ( c += a[i] * n ) % 10000;
c /= 10000;
}
if ( a[i] = c ) m++;
}
printf ( "%d", a[m] );
for( c = m - 1; c >= 0; c-- )
{
printf( "%04d", a[c] );
}
printf ("\n"); system ("pause");
}
谁看懂的把原理写一下
http://blog.csdn.net/lzmtw/archive/2006/11/02/1363174.aspx
好象可以算任意大的阶乘.当然,它没有什么算法可言,只是能算出来.这是算1到1000阶乘的
http://blog.csdn.net/lzmtw/archive/2006/11/02/1361687.aspx
大数问题
【参考程序】
#include <stdio.h>
#include <string.h>
int main()
{
int a[20000]; //模拟高精度数
int k; //a数组所用的长度
int i , j; //循环变量
int n; //求n的阶乘
scanf("%d" , &n);
if (n == 0) printf("0\n"); //0!单独判断
else
{
memset(a , 0 , sizeof(a)); //数组清0
a[0] = 1; //个位初始值为1
k = 1;
for (i = 2; i <= n; i ++)
{
for (j = 0; j < k; j ++) //高精度数做乘法
a[j] *= i;
for (j = 0; j < k; j ++) //模拟进位,保证每一位都只储存一位数
if (a[j] > 9)
{
a[j + 1] += a[j] / 10;
a[j] %= 10;
}
while (a[k] > 0) //扩展k
{
a[k+1] += a[k] / 10;
a[k] %= 10;
k ++;
}
}
for (i = k-1; i >= 0; i --) printf("%d" , a[i]); //高精度数输出
printf("\n");
}
return 0;
}
#include <stdio.h>
#define N 100 //计算N的阶乘,修改N的定义可计算200000以内任意数的阶乘
int a[N]={1},n=N,i,c,m;void main(){
for(;n>1;(a[i]=c)>0?m++:0,n--)
for(c=i=0;i <=m;i++)a[i]=(c+=a[i]*n)%10,c/=10;
for(;m>=0;)printf("%d",a[m--]);} 1. 声明,这个程序只是在尝试如何写短小的程序,不推荐大家这样写代码。
2. 关于这个程序是如何一步一步压缩到这种方式的,请参阅http://blog.csdn.net/liangbch/archive/2008/11/05/3230428.aspx另外,这个博客(http://blog.csdn.net/liangbch/category/292924.aspx)有几篇篇关于 大数阶乘之计算从入门到精通 的系列文章,感兴趣者可参阅之。
import.java.util.ArrayList;public class UseArrayList{
public static void main(String [] args){
ArrayList alist =factorial(100);
for(int i=0 ; i<=100 ; i++ ){
System.out.println(i + "! = " +alist.get(i));
}
}
public static ArrayList factorial(int x){
ArrayList alist = new Arraylist();
alist.add(BigInteger.valueOf(1));
for(int i=alist.size() ; i<x ; i++){
BigInteger lastfact = (BigInteger)alist.get(i-1);
BigInteger nextfact = last.multiply(BigInteger.valueOf(i));
alist.add(nextfact);
}
return alist;
}
}
for (int i=obj.Leagth-1;i>0;i--)
{
double a=obj*i;
}
console.WriteLine(a);
{
Double result=1; for (int i = 1; i <= number; i++)
{
result = result * i;
} return result;
}
public double DiGuiCalculate(int number)
{
if (number < 1)
{
int i = 1;
return i;
}
return number * DiGuiCalculate(number - 1);
}