笔试题:计算N的阶乘

简介:

public class test 
{
    //简单起见,不考虑负号的情况
    private static String multipy(String num1, String num2)
    {//大数乘法
        String result = "0";
        int i,j,n1,n2;
        int len1 = num1.length();
        int len2 = num2.length();
        if (len1 < len2)
        {
            for (i = len1 -1; i >=0; --i)
            {
                n1 = num1.charAt(i) - '0';
                String sum = "0";
                for (j = 0; j < n1; ++j)
                {
                    sum = add(sum,num2);
                }
                StringBuilder tmpSB = new StringBuilder(sum);
                for (j = i; j < len1 -1; ++j)
                {
                    tmpSB.append("0");
                }
                result = add(result,tmpSB.toString());
            }
        }
        else
        {
            for (i = len2 -1; i >=0; --i)
            {
                n2 = num2.charAt(i) - '0';
                String sum = "0";
                for (j = 0; j < n2; ++j)
                {
                    sum = add(sum,num1);
                }
                StringBuilder tmpSB = new StringBuilder(sum);
                for (j = i; j < len2 -1; ++j)
                {
                    tmpSB.append("0");
                }
                result = add(result,tmpSB.toString());
            }
        }

        return result;
    }
    private static String add(String num1, String num2)
    {//大数加法
        String result = "";
        int len1 = num1.length();
        int len2 = num2.length();
        int nAddOn = 0;//进位
        int i,j,n1,n2,sum;
        StringBuilder sb = new StringBuilder();
        for (i = len1 - 1,j = len2 - 1 ; i >= 0 && j >= 0; --i,--j)
        {
            n1 = num1.charAt(i) - '0';
            n2 = num2.charAt(j) - '0';
            sum = n1 + n2 + nAddOn;
            
            if (sum >= 10)
            {
                nAddOn = 1;
            }
            else
            {
                nAddOn = 0;
            }
            sb.append(sum % 10);
        }
        if (len1 > len2)
        {//第一个有剩余
            for (; i >= 0; --i)
            {
                n1 = num1.charAt(i) - '0';
                sum = n1 + nAddOn;
                if (sum >= 10)
                {
                    nAddOn = 1;
                }
                else
                {
                    nAddOn = 0;
                }
                sb.append(sum % 10);
            }
        }
        else if (len2 > len1)
        {//第二个有剩余
            for (; j >= 0; --j)
            {
                n2 = num2.charAt(j) - '0';
                sum = n2 + nAddOn;
                if (sum >= 10)
                {
                    nAddOn = 1;
                }
                else
                {
                    nAddOn = 0;
                }
                sb.append(sum % 10);
            }
        }
        
        if (nAddOn > 0)
        {
            sb.append(nAddOn);
        }
        
        sb.reverse();
        result = sb.toString();
        return result;
    }
    private static String factorial(int n)
    {
        String result = "1";
        for (int i = n; i >= 2; --i)
        {
            result = multipy(result,String.valueOf(i));
        }
        return result;
    }
    public static void main(String[] args) throws Exception
    {
        //计算100的阶乘!
        System.out.println(factorial(100));
    }

}
复制代码
再来个c++版做对比
复制代码

//简单起见,不考虑负号的情况

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

string add(string num1, string num2)
{//大数加法
    string result = "";
    int len1 = num1.length();
    int len2 = num2.length();
    int nAddOn = 0;//进位
    int i,j,n1,n2,sum;
    vector<char> tmpSum;

    for (i = len1 - 1,j = len2 - 1 ; i >= 0 && j >= 0; --i,--j)
    {
        n1 = num1[i] - '0';
        n2 = num2[j] - '0';
        sum = n1 + n2 + nAddOn;

        if (sum >= 10)
        {
            nAddOn = 1;
        }
        else
        {
            nAddOn = 0;
        }
        tmpSum.push_back(sum % 10 + '0');
    }
    if (len1 > len2)
    {//第一个有剩余
        for (; i >= 0; --i)
        {
            n1 = num1[i] - '0';
            sum = n1 + nAddOn;
            if (sum >= 10)
            {
                nAddOn = 1;
            }
            else
            {
                nAddOn = 0;
            }
            tmpSum.push_back(sum % 10 + '0');
        }
    }
    else if (len2 > len1)
    {//第二个有剩余
        for (; j >= 0; --j)
        {
            n2 = num2[j] - '0';
            sum = n2 + nAddOn;
            if (sum >= 10)
            {
                nAddOn = 1;
            }
            else
            {
                nAddOn = 0;
            }
            tmpSum.push_back(sum % 10 + '0');
        }
    }

    if (nAddOn > 0)
    {
        tmpSum.push_back(nAddOn + '0');
    }
    reverse(tmpSum.begin(),tmpSum.end());
    copy(tmpSum.begin(),tmpSum.end(),back_inserter(result));
    return result;
}

string multipy(string num1, string num2)
{//大数乘法
    string result = "0";
    int i,j,n1,n2;
    int len1 = num1.length();
    int len2 = num2.length();
    if (len1 < len2)
    {
        for (i = len1 -1; i >=0; --i)
        {
            n1 = num1[i] - '0';
            string sum = "0";
            for (j = 0; j < n1; ++j)
            {
                sum = add(sum,num2);
            }

            string tmpSB(sum);
            for (j = i; j < len1 -1; ++j)
            {
                tmpSB.append("0");
            }
            result = add(result,tmpSB);
        }
    }
    else
    {
        for (i = len2 -1; i >=0; --i)
        {
            n2 = num2[i] - '0';
            string sum = "0";
            for (j = 0; j < n2; ++j)
            {
                sum = add(sum,num1);
            }
            string tmpSB(sum);
            for (j = i; j < len2 -1; ++j)
            {
                tmpSB.append("0");
            }
            result = add(result,tmpSB);
        }
    }

    return result;
}


string factorial(int n)
{
    string result = "1";
    char buff[100];
    for (int i = n; i >= 2; --i)
    {
        result = multipy(result,_itoa(i, buff,10));
    }
    return result;
}

int main()
{
    int N;
    while (cin >> N)
    {
        cout << factorial(N).c_str() << endl;
    }
    return 0;
}
复制代码



本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2009/05/03/1448245.html,如需转载请自行联系原作者

目录
相关文章
|
2月前
|
C语言
C语言之完数、素数、回文数合集
C语言之完数、素数、回文数合集
|
2月前
|
C语言
c语言编程练习题:7-41 计算阶乘和
c语言编程练习题:7-41 计算阶乘和
123 0
|
12月前
1172:求10000以内n的阶乘
1172:求10000以内n的阶乘
113 0
|
12月前
循环习题—阶乘以及阶乘相加
循环习题—阶乘以及阶乘相加
|
11月前
|
C语言
C语言练习---【求素数】(一篇带你掌握素数求解)
C语言练习---【求素数】(一篇带你掌握素数求解)
100 0
C语言练习---【求素数】(一篇带你掌握素数求解)
|
10月前
|
C语言
C语言中n的阶乘的两种思路
C语言中n的阶乘的两种思路
|
12月前
辗转相除法(C语言版)(以求两个数的公约数为例)
辗转相除法(C语言版)(以求两个数的公约数为例)
|
11月前
华华教月月做数学 两种方法: (快速幂+快速乘) (__int128+快速幂)
华华教月月做数学 两种方法: (快速幂+快速乘) (__int128+快速幂)
40 0
|
算法 C语言
C语言题解——最小公倍数的三种求法(含最大公约数)
最小公倍数是指能同时将两数整除的最小倍数,而最大公约数是则是能被两数同时整除的最小因数。最小公倍数有个特点,就是最小为两数中的较大值,最大为两数的乘积;最小公倍数则是最小为1,最大为两数中较小值(如果两数相同,那么最大公约数、最小公倍数是它们本身)🎉🎉🎉
258 1
C语言题解——最小公倍数的三种求法(含最大公约数)
|
算法 程序员 C语言
C语言基础(有关三个数比较大小、冒泡排序、最大公约数、和有关某个数x的绝对值的n次方除于n的阶乘问题的函数求解法;和阶乘函数递归方法;和数组作函数参数的
C语言基础(有关三个数比较大小、冒泡排序、最大公约数、和有关某个数x的绝对值的n次方除于n的阶乘问题的函数求解法;和阶乘函数递归方法;和数组作函数参数的