阶乘函数只返回C++中输入的答案
Factorial function only returning inputted answer in C++?
我必须创建一个程序来计算任何数字的阶乘,问题是如果我输入20以上的任何数字,它只返回该数字。在我的else-if语句中,可能是什么导致了这种情况,有更好的方法来解决吗?(此函数在main中被调用,并且在num<=20时工作(
void factorial() {
//User input for number
long long num;
std::cout << "Input any positive integer to find its factorial: ";
std::cin >> num;
unsigned long long numFact = 1;
if (num <= 20) {
while (num > 0) {
numFact = numFact * num;
num = num - 1;
}
std::cout << numFact;
}
else if (num > 20) {
std::vector<int> multFactorial;
//stores num as seperate elements in vector multFactorial
while (num > 0) {
int remain = num % 10;
num = num / 10;
multFactorial.insert(multFactorial.begin(), remain);
}
std::vector<int> answer;
std::vector<int> answerFinal;
//Manually multiplies elements in multFactorial
//Then adds new vectors created by multiplying to get final answer
//Repeats until factorial is solved
//Ex: 21 * 20; 0 * 1 and 0 * 2 stored as {0 , 0}
//2*1 and 2*2 stored as {4, 2, 0}
//Vectors will be addes to get {4, 2, 0} and then that will be multiplied
by 19 until num = 1
while (num > 1) {
for (int i = multFactorial.size() - 1; i >= 0; i--) {
int remain1 = ((num - 1) % 10) * multFactorial[i];
answer.insert(answer.begin(), remain1);
int remain2 = (((num - 1) / 10) * multFactorial[i]);
answerFinal.insert(answerFinal.begin(), remain2);
}
answerFinal.insert(answerFinal.begin(), 0);
//Adds vectors to get final value seperate as digits
for (int i = multFactorial.size() - 1; i >= 0; i--) {
multFactorial[i] = answer[i] + answerFinal[i];
}
num = num - 1;
}
//Prints what should be the factorial of the number input
for (size_t i = 0; i < multFactorial.size(); i++) {
std::cout << multFactorial[i];
}
}
}
大数的阶乘会产生大数。这可以在C、C++等语言中通过将结果放入任意长度的字符串来实现。
这里有一个类似于你的算法。
https://www.geeksforgeeks.org/factorial-large-number/
最好的建议是对照这个检查你的代码。如果有调试器,请使用调试器,并逐行执行代码。如果未打印出中间结果,并与预期结果进行比较。
编辑:根据评论,上面引用的代码与下面的代码相似,以防将来链接断开。
// C++ program to compute factorial of big numbers
#include<iostream>
using namespace std;
// Maximum number of digits in output
#define MAX 100 // change to whatever value you need
int multiply(int x, int res[], int res_size);
// Calculate factorial of large number
void factorial(int n)
{
int res[MAX];
// Initialize result
res[0] = 1;
int res_size = 1;
// Apply factorial formula
for (int x=2; x<=n; x++)
res_size = multiply(x, res, res_size);
// print out the result
cout << "Factorial is n";
for (int i=res_size-1; i>=0; i--)
cout << res[i];
}
// Multiplies x with the number represented by res[].
// res_size is size of res[] or number of digits in the
// number represented by res[].
int multiply(int x, int res[], int res_size)
{
int carry = 0; // Initialize carry
// One by one multiply n with individual digits of res[]
for (int i=0; i<res_size; i++)
{
int prod = res[i] * x + carry;
// Store last digit of 'prod' in res[]
res[i] = prod % 10;
// Put rest in carry
carry = prod/10;
}
// Put carry in res and increase result size
while (carry)
{
res[res_size] = carry%10;
carry = carry/10;
res_size++;
}
return res_size;
}
// Main program
int main()
{
//put code here to read a number
factorial(50); // take 50 for example
return 0;
}
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- 仅通过C++中数组初始化的不同方法,即可在同一输入上获得两个不同的答案
- 阶乘函数只返回C++中输入的答案
- 尝试检查输入的文本与我自己的自定义答案
- 欧拉项目#8:C++错误的答案(为一个小的炮制输入给出正确的答案)
- 按下Enter时如何输入错误消息,但没有给出任何答案
- 为什么意外值6295648在输出中带有答案11110001?在这里输入是241和2
- 创建一个程序,要求用户输入两个数字,并有三个可能的答案.代码未编译.数字(作为int)
- 快速排序算法,一些特定输入序列的错误答案和分段错误
- 我如何让我的部分代码循环并重复一个问题,直到答案是有效的输入C++
- 输入答案时,如何区分大小写无关紧要
- C++正在检查用户是否输入了有效答案
- 如何使用Yes/No来输入你的答案
- 为什么大的输入会得到错误的答案