c++简单题,跪求高手解答

ADFS(digitalfactorialsum)numberisfoundbysummingthefactorialofeverydigitofapositiveint... A DFS(digital factorial sum) number is found by summing the factorial of every digit of a positive integer.

For example ,consider the positive integer 145 = 1!+4!+5!, so it's a DFS number.

Now you should find out all the DFS numbers in the range of int( [1, 2147483647] ).

There is no input for this problem. Output all the DFS numbers in increasing order. The first 2 lines of the output are shown below.

Input
no input

Output
Output all the DFS number in increasing order.

Sample Output

1
2
......
不知道错在哪?
#include<iostream>
using namespace std;
int f(int n)
{
int t=n;
if(n==0)
return 1;
else
{
while(--n)
t*=n;
return t;
}
}
int main()
{
int n,i,t=0,x;
cout<<'1'<<endl;
cout<<'2'<<endl;
for(i=100;i<=2147483647;i++)
{
n=i;
while(n>9)
{
x=n%10;
t+=f(x);
n/=10;
}
t+=f(n);
if(t==i)
cout<<i<<endl;
else continue;
}
return 0;
}
展开
 我来答
hendlon
2009-08-31 · TA获得超过297个赞
知道小有建树答主
回答量:544
采纳率:100%
帮助的人:431万
展开全部
先不给你调整个程序,给你提出几个问题:

函数f(int n),你是想返回阶乘的值对吧?
阶乘的算法是n! =n*(n-1)*(n-2).....*2*1
你随便代个数进的函数就发现你的函数写错了。
t是阶乘的基数,应该赋初值为1,而不是为 n
而 t*=n 表达式没错,但是while循环条件错了,不是 --n而是 n--
仔细想想。

在主函数中,很明显的一个错误就是,你的for循环中,i越界了,int 型变量的值范围你找找书看,好象是在 65535,过了就越界了。

然后,你的main函数的中的变量名完全不用跟你函数的参数同名或者相似。否则容易引起自己和机器的误解,如果必须用的时候,都是要加this指针的。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式