要程序的这里有:
#include
#include
int res[3] = {};
int *p = res;
int main(){
int func(int);
for(int i=100;i<1000;i++){
//各位数字x的立方和
p = res; //循环后应该重置到res数组首地址
if(func(i) == i){
printf("%d ",i);
}
}
}
int func(int num){
int r = 0;
if(num>9){
func(num/10);
}
*p = num % 10;
p++;
if(p == res+3){ //指向末尾+1,存入数组结束
p = res;
for(int i=0;i<3;i++){
r += pow(*p,3);
p++;
}
return r;
}
return 0;
}
如有帮助,请点赞支持原创QVQ
你不要过程呀,那结果是:
水仙花数共有4个,分别为:153、370、371、407
1.
for i=100 to 999
a=int(i/100)
c=i mod 10
b=i-a*100-c
if i=a*a*a+b*b*b+c*c*c then print i
next i
end
vb
Private Sub Command1_Click()
Dim s As Integer: Dim n As Integer
For s = 100 To 999
If (Val(Left(s, 1))) ^ 3 + (Val(Mid(s, 2, 1))) ^ 3 + (Val(Right(s, 1))) ^ 3 = Val(s) Then
Print s;
n = n + 1
If n Mod 8 = 0 Then Print
End If
Next s
Print
Print "共有水仙花数" & n & "个"
End Sub
#include
#include
int is_daffodils(int n)
{
int nums=n;
int a,b,c;
int flag = 0;
a = n % 10;
n/=10;
b = n % 10;
n/=10;
c = n % 10;
if(nums == a*a*a+b*b*b+c*c*c)
{
flag = 1;
}
return flag;
}
int main()
{
for(int i =100;i<=999;i++)
{
if(is_daffodils(i))
{
printf("水仙花数:%d\n",i);
}
}
return 0;
}