C# 1000的阶乘

转载自:http://blog.sina.com.cn/s/blog_4baa2c250100ijpn.html


1000的阶乘是一个非常之大的数字,在看到这个问题的第一个反应就是,直接运算肯定会溢出,事实也如此,我们如何解决这个问题呢?
我们小学的时候就会计算这样的乘法:123*9
三九二十七进二写七                            7    2
二九十八  十八加二等于二十  进二写零           0    2
一九得九  九加二十一       进一写一           1     1

最后的结果是1107

如何计算阶乘结果的位数?
方法一:
log1+log2+log3+log4+...+logn 取整加1
方法二:
斯特林公式

按照这样的步骤,把每次得到的结果存放到数组里,考虑到无法确定数组长度,采用ArrayList
实现如下:

Java代码  复制代码
  1. public static void factorial(int value) {   
  2.         ArrayList result new ArrayList();   
  3.         int carryBit 0;   
  4.   
  5.         result.add(new Integer(1));   
  6.         for (int out 2out <= value; out++) {   
  7.             for (int in 0in result.size(); in++) {   
  8.                 int temp ((Integer) result.get(in)).intValue() out   
  9.                         carryBit;   
  10.                 result.set(in, new Integer(temp 10));   
  11.                 carryBit temp 10;   
  12.             }   
  13.             while (carryBit != 0{   
  14.                 result.add(new Integer(carryBit 10));   
  15.                 carryBit carryBit 10;   
  16.             }   
  17.         }   
  18.         StringBuffer sb=new StringBuffer(result.size());   
  19.         for(int i=0;i<result.size();i++)   
  20.         {   
  21.             sb.append(result.get(i));   
  22.         }   
  23.         sb=sb.reverse();   
  24.         System.out.println("result="+sb);   
  25.         System.out.println("结果位数"+result.size());   
  26.      

C语言版:(自己写的)

#include<iostream>

using namespace std;

void factorial(int value);

void main()
{

  factorial(10); 
}

void factorial(int value)
 
     
 int result[10000]={0};
 int result_size=1;//实时更新result的实际位数

 int carryBit = 0;   //进位的值
 
    result[0]=1;//初始结果为1
 
 for (int out = 2; out <= value; out++) {  
         for (int in = 0; in < result_size; in++) {  
                int temp = result[in] * out  
                        + carryBit;  
                result[in]=temp % 10;  
                carryBit = temp / 10;  
            }  
            while (carryBit != 0) { 
                result[result_size]=carryBit % 10;  
                result_size++;
                carryBit = carryBit / 10;  
            }  
        }
 
      cout<<value<<"的阶乘结果为:"<<endl;
        for(int i=0;i<result_size;i++)  
        {  
            cout<<result[i];
        }  
  
 }

C#:自己写的:

public static void Factorial(int value)
        {
            int[] result = new int[10000];
            int result_size = 1;//实时更新result的实际位数
            int carryBit = 0;   //进位的值

            result[0] = 1;//初始结果为1

            for (int i = 2; i <= value; i++) {
                for (int j = 0; j < result_size; j++) {
                    int temp = result[j] * i 
                        +carryBit;
                    result[j] = temp % 10;
                    carryBit = temp / 10;
                }
                while (carryBit != 0)
                {
                    result[result_size] = carryBit % 10;
                    result_size++;
                    carryBit = carryBit / 10;
                }
            }
            Console.WriteLine("{0}的阶乘结果:", value);
            for (int i = 0; i < result.Length; i++)
            {
                Console.Write(result[i]);
            }
        }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值