return 1 return 0 在阶乘的情况下
return 1 return 0 in case of factorial
我需要代码的一般解释:
案例1)
在这个阶乘函数中,如果 num
是 0,那么 return 0! 是 1 吗?
案例2)
如果数字大于等于 1,它 return fact
,它的阶乘值是多少?
我知道 return 1
和 return 0
都是为了成功生成结果。
那为什么我不能做 return 0,在这种情况下?
double factorial(int num)
{
int fact = 1;
int i = 1;
if (num == 0)
return 1;
else
while (num >= i)
{
fact = fact*i;
i++;
}
return fact;
#include <iostream>
using namespace std;
int factorial(int num) //I changed this to return int since you are taking int and int*int will always be int
{
int fact = 1;
//int i = 1; //dont need this
if (num == 0)
return fact; //You can just say return `fact` or `return 1` - i like to make my code readable - s I used `return fact`
//I also prefer to set the value of fact as 1 here and return the 1 at bottom so we only have one return statement
//but thats just me - having 2 return statements should be fine if used wisely
/*while (num >= i) //thispart was wrong i reedited it into a better and more efficient code below
{
fact = fact*i;
i++;
}*/
else
{
while(num>1) // so lets say we enter 4 - 4 is larger than 1
{
fact*=num; //first step through it will be fact = fact * num; fact is 1 at first loop so it will be 1 * 4 and we put that value into fact
num--; //here we set num to 3 for next loop and we repeat :D
}
}
return fact; //here we return the value
}
int main() //just a normal main
{
int number;
cout<<"Enter number: \n";
cin>>number;
cout<<"Factorial of "<<number<<" is "<<factorial(number);
return 0;
}
我认为你的问题非常好,作为一名初级程序员,当我看到这样的问题时,它也对我有帮助。
希望这可以帮助!祝你好运!
我需要代码的一般解释:
案例1)
在这个阶乘函数中,如果 num
是 0,那么 return 0! 是 1 吗?
案例2)
如果数字大于等于 1,它 return fact
,它的阶乘值是多少?
我知道 return 1
和 return 0
都是为了成功生成结果。
那为什么我不能做 return 0,在这种情况下?
double factorial(int num)
{
int fact = 1;
int i = 1;
if (num == 0)
return 1;
else
while (num >= i)
{
fact = fact*i;
i++;
}
return fact;
#include <iostream>
using namespace std;
int factorial(int num) //I changed this to return int since you are taking int and int*int will always be int
{
int fact = 1;
//int i = 1; //dont need this
if (num == 0)
return fact; //You can just say return `fact` or `return 1` - i like to make my code readable - s I used `return fact`
//I also prefer to set the value of fact as 1 here and return the 1 at bottom so we only have one return statement
//but thats just me - having 2 return statements should be fine if used wisely
/*while (num >= i) //thispart was wrong i reedited it into a better and more efficient code below
{
fact = fact*i;
i++;
}*/
else
{
while(num>1) // so lets say we enter 4 - 4 is larger than 1
{
fact*=num; //first step through it will be fact = fact * num; fact is 1 at first loop so it will be 1 * 4 and we put that value into fact
num--; //here we set num to 3 for next loop and we repeat :D
}
}
return fact; //here we return the value
}
int main() //just a normal main
{
int number;
cout<<"Enter number: \n";
cin>>number;
cout<<"Factorial of "<<number<<" is "<<factorial(number);
return 0;
}
我认为你的问题非常好,作为一名初级程序员,当我看到这样的问题时,它也对我有帮助。 希望这可以帮助!祝你好运!