求n的阶乘n为10000c语言,求10000以内n的阶乘。该怎么解决

本文展示了使用C++编程实现字符串表示的整数比较、加法、减法和乘法操作,以及计算字符串表示的整数阶乘的功能。代码中包括了比较函数COMPARE、加法函数PLUS、减法函数MINUS和乘法函数MULTIPLY,以及一个计算阶乘的辅助函数factorial。这些函数处理字符串形式的数字,可以用于数值计算的实践应用。
摘要由CSDN通过智能技术生成

C/C++ code#include

#include

#include

using namespace std;

int COMPARE(string number1, string number2) {

int i,j;

int length1 = number1.size();

int length2 = number2.size();

if(number1.size() == 0) number1 = "0";

if(number2.size() == 0) number2 = "0";

j = 0;

for(i = 0; i < length1; ++i) {

if(number1[i] == '0') ++j;

else break;

}

number1 = number1.substr(j);

j = 0;

for(i = 0; i < length2; ++i) {

if(number2[i] == '0') ++j;

else break;

}

number2 = number2.substr(j);

length1 = number1.size();

length2 = number2.size();

if(length1 > length2) {

return 1;

} else if(length1 == length2) {

if(number1.compare(number2) > 0) {

return 1;

} else if(number1.compare(number2) == 0) {

return 0;

} else {

return -1;

}

} else {

return -1;

}

return 0;

}

string PLUS(string number1,string number2) {

int i;

int length1 = number1.size();

int length2 = number2.size();

string result="";

reverse(number1.begin(), number1.end());

reverse(number2.begin(), number2.end());

for(i = 0; i < length1 && i < length2; i++) {

char c = (char)(number1[i] + number2[i] - 48);

result = result + c;

}

while(i < length1) {

result = result + number1[i];

++i;

}

while(i < length2) {

result = result + number2[i];

++i;

}

int carry = 0;

for(i = 0; i < (int)result.size(); ++i) {

int value = result[i] - 48 + carry;

result[i] = (char)(value % 10 + 48);

carry = value / 10;

}

if(carry !=0 ) {

result = result + (char)(carry + 48);

}

for(i = result.size() - 1; i >= 0; i--) {

if(result[i] != '0') break;

}

result = result.substr(0, i + 1);

reverse(result.begin(), result.end());

if(result.length() == 0) result = "0";

return result;

}

string MINUS(string number1,string number2) {

int i;

string result = "";

int length1 = number1.size();

int length2 = number2.size();

if(COMPARE(number2,number1) > 0) {

return "-" + MINUS(number2, number1);

}

reverse(number1.begin(),number1.end());

reverse(number2.begin(),number2.end());

for(i = 0; i < length1 && i < length2; i++) {

char c = number1[i] - number2[i] + 48;

result = result + c;

}

if(i < length1) {

for(; i < length1; i++) {

result = result + number1[i];

}

}

int carry = 0;

for(i = 0; i < (int)result.length(); i++) {

int value = result[i] - 48 + carry;

if(value < 0) {

value = value + 10;

carry = -1;

} else carry = 0;

result[i]=(char)(value + 48);

}

for(i = result.size() - 1; i >= 0; i--) {

if(result[i] != '0')break;

}

result = result.substr(0, i+1);

reverse(result.begin(), result.end());

if(result.length()==0) result = "0";

return result;

}

string MULTIPLY(string number1, string number2) {

int i, j;

int *iresult;

int length1 = number1.size();

int length2 = number2.size();

string result = "";

reverse(number1.begin(), number1.end());

reverse(number2.begin(), number2.end());

iresult = (int*)malloc(sizeof(int) * (length1 + length2 + 1));

memset(iresult, 0, sizeof(int) * (length1 + length2 + 1));

for(i = 0; i < length1; i++) {

for(j = 0; j < length2; j++) {

iresult[i+j] += ((number1[i] - 48) * (number2[j] - 48));

}

}

int carry = 0;

for(i = 0; i < length1 + length2; i++) {

int value = iresult[i] + carry;

iresult[i] = value % 10;

carry = value / 10;

}

for(i = length1 + length2 - 1; i >= 0; i--) {

if(iresult[i] != 0)break;

}

for(; i >= 0; i--) {

result = result + (char)(iresult[i]+48);

}

free(iresult);

if(result == "") result = "0";

return result;

}

string factorial(string n) {

string temp = "1";

string i;

for(i = "1"; COMPARE(i, n) <= 0; i = PLUS(i, "1")) {

temp = MULTIPLY(temp, i);

}

return temp;

}

int main(void) {

cout << factorial("10000") << endl;

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值