# 笔试题：计算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 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';
if (sum >= 10)
{
}
else
{
}
sb.append(sum % 10);
}
}
else if (len2 > len1)
{//第二个有剩余
for (; j >= 0; --j)
{
n2 = num2.charAt(j) - '0';
if (sum >= 10)
{
}
else
{
}
sb.append(sum % 10);
}
}

{
}

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

25999 0

7922 0

2662 0

10311 0

9682 0

11375 0
windows server 2008阿里云ECS服务器安全设置

8223 0

6727 0

21064 0
+关注
4716

0

《2021云上架构与运维峰会演讲合集》

《零基础CSS入门教程》

《零基础HTML入门教程》