
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;
} 展开
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;
} 展开
1个回答
展开全部
先不给你调整个程序,给你提出几个问题:
函数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指针的。
函数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指针的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询