- 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;
- }