# 笔试题：计算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)
{
}
StringBuilder tmpSB = new StringBuilder(sum);
for (j = i; j < len1 -1; ++j)
{
tmpSB.append("0");
}
}
}
else
{
for (i = len2 -1; i >=0; --i)
{
n2 = num2.charAt(i) - '0';
String sum = "0";
for (j = 0; j < n2; ++j)
{
}
StringBuilder tmpSB = new StringBuilder(sum);
for (j = i; j < len2 -1; ++j)
{
tmpSB.append("0");
}
}
}

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)
{
}
else
{
}
sb.append(sum % 10);
}
if (len1 > len2)
{//第一个有剩余
for (; i >= 0; --i)
{
n1 = num1.charAt(i) - '0';
sum = n1 + nAddOn;
if (sum >= 10)
{
}
else
{
}
sb.append(sum % 10);
}
}
else if (len2 > len1)
{//第二个有剩余
for (; j >= 0; --j)
{
n2 = num2.charAt(j) - '0';
sum = n2 + nAddOn;
if (sum >= 10)
{
}
else
{
}
sb.append(sum % 10);
}
}

if (nAddOn > 0)
{
}

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

}

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

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

if (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)
{
}

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

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

9876 0

18843 0

17168 0

25161 0

21652 0

19147 0
+关注
4716

0

JS零基础入门教程（上册）