计算一个3位数的数字的阶乘和 我编写了C++代码,计算三位数字中的阶乘的和,但不起作用:

计算一个3位数的数字的阶乘和 我编写了C++代码,计算三位数字中的阶乘的和,但不起作用:,c++,factorial,C++,Factorial,输出为: 返回的进程-1073741571(0xC00000FD)执行时间:9.337秒 按任意键继续 #include <iostream> #include <exception> using namespace std; unsigned fact(int n) { if (n == 1|n==0) return 1; else return n * fact(n - 1); } int main() { in

输出为:

返回的进程-1073741571(0xC00000FD)执行时间:9.337秒 按任意键继续

#include <iostream>
#include <exception>
using namespace std;
unsigned fact(int n)
{
    if (n == 1|n==0)
        return 1;
    else
        return n * fact(n - 1);
}
int main()
{
    int num;
    int sum=0;
    int tmp;
    cout<<"Enter 3 digit number:\n";
    cin>>num;
    if(num<99|num>999)
    {
        cout<<"Not a 3 digit number!";
        return (1);
    }
    tmp = num%100;
    sum = sum+ fact(tmp);
    tmp = num%10;
    sum = sum+ fact(tmp);
    tmp = num%1;
    sum = sum+ fact(tmp);

cout<<"Sum of factorial of digits in a number:"<<sum;
return(0);
}
#包括
#包括
使用名称空间std;
无符号事实(整数n)
{
如果(n==1 | n==0)
返回1;
其他的
返回n*事实(n-1);
}
int main()
{
int-num;
整数和=0;
int tmp;
库特南;
如果(num999)
{

num
的数字不是
num%100
num%10、
num%1
。是什么让你产生了这个想法

num=567
为例

num%100=67

num%10=7

num%1=0


你需要再仔细考虑一下。

我忘了如何选择数字的位数,现在可以了:

#include <iostream>
#include <exception>
using namespace std;
unsigned fact(int n)
{
    if (n == 1||n==0)
        return 1;
    else
        return n * fact(n - 1);
}
int main()
{
    int num;
    int sum=0;
    int tmp;
    cout<<"Enter 3 digit number:\n";
    cin>>num;
    if(num<=99|num>999)
    {
        cout<<"Not a 3 digit number!";
        return (1);
    }
    tmp = num%10;
    sum = sum+ fact(tmp);
    tmp = num/10%10;
    sum = sum+ fact(tmp);
    tmp = num/100%10;
    sum = sum+ fact(tmp);

cout<<"Sum of factorial of digits in a number:"<<sum;
return(0);
}
#包括
#包括
使用名称空间std;
无符号事实(整数n)
{
如果(n==1 | | n==0)
返回1;
其他的
返回n*事实(n-1);
}
int main()
{
int-num;
整数和=0;
int tmp;
库特南;
如果(num999)
{

你的
事实
函数在0时能做什么?编辑为包含0;我想你的意思是
|
而不是
|
(两次都是-尽管老实说它们无论如何都能工作,但这有点奇怪)。无论如何,您更大的问题是获取数字的方式,
num%100
可能相当大……您输入的数字是多少,还是没有达到接受输入的程度?还有,
num