水仙花数是指一种三位数,其各个数之立方和等于该数。
水仙花数共有4个,分别为:153、370、371、407
( 例如:1^3 + 5^3 + 3^3 = 153 )
C语言的"水仙花数"实现代码:
void main()
{
int ge,shi,bai,number;
for(number=100;number<1000;number++)
{
bai=number/100;
shi=(number%100)/10;
ge=number%10;
if(number==bai*bai*bai+shi*shi*shi+ge*ge*ge) printf("\nnumber=%d\n",number);
}
getch();
}
Visual Basic的"水仙花数"实现代码:
Private Sub Form_Click()
Dim a, b, c As Integer 'a(个)b(十)c(百)
For a = 0 To 9
For b = 0 To 9
For c = 1 To 9
If a ^ 3 + b ^ 3 + c ^ 3 = a + 10 * b + 100 * c Then
MsgBox 100 * c + 10 * b + a
End If
Next c
Next b
Next a
End Sub
FORTRAN的"水仙花数"实现代码:
WRITE(*,30)
DO 10 K=100,999
IA=K/100
IB=MOD(K,100)/10
IC=MOD(K,10)
IF(K.NE.IA**3+IB**3+IC**3) GOTO 10
WRITE(*,20)K, IA,IB,IC
10 CONTINUE
20 FORMAT(5X,4I4)
30 FORMAT(5X,18HN=I**3+J**3+K**3)
STOP
END
pascal:
program shuixianhuashu;
var
a,b,c:integer;
begin
for a:=1 to 9 do
for b:=0 to 9 do
for c:=0 to 9 do
if a*a*a+b*b*b+c*c*c=100*a+10*b+c then writeln(100*a+10*b+c);
用C语言for循环求解该问题的代码
#include "Stdio.h"
int main(void)
{
int a,b,c;
for(a=1;a<=9;a++)
for(b=0;b<=9;b++)
for(c=0;c<=9;c++)
if(100*a+10*b+c==a*a*a+b*b*b+c*c*c)
printf("%d%d%d ",a,b,c);
}
end.
C++编译器上的水仙花数实现代码
#include
using namespace std;
void main()
{
int a=100;
int q,w,e;
for(a;a<999;++a)
{
q=a/100;
w=(a-q*100)/10;
e=(a-q*100-w*10);
if(a==q*q*q+w*w*w+e*e*e)
cout<}
//修改一下
#include
#include
using namespace std;
struct Range{
int min;
int max;
};
// 计算水仙花
void fun(int m, int n);
int main()
{
vector
cout<<"sample input"<
int a;
//输入非数字来结束输入,如'e'
while (cin>>a)
{
Range r;
r.min = a;
cin>>r.max;
myrange.push_back(r);
}
//输出
cout<<"sample output"<
{
Range rr = myrange.at(a);
fun(rr.min, rr.max);
}
return 0;
}
void fun(int m, int n)
{
bool isExist = false; //区间内是否存在水仙数
int num;
int i, j, k;
for(num=m; num<=n; num++)
{
i = num/100; //百位
j = num/10%10; //十位
k = num%10; //个位
if(num==(i*i*i+j*j*j+k*k*k))
{
cout<
}
}
if (!isExist)
{
cout<<"no";
}
cout<
不用那么复杂,我这个比较简洁
#include"stdio.h"
void fun(int *a,int n)
{int i,j;
for(i=0,j=n-1;i
{t=a[i];a[i]=a[j];a[j]=t;}
for(i=0;i<=n-1;i++)
printf("%d",a[i]);}
void main()
{int a[10],i;
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
for(i=0;i<=n-1;i++)
printf("%d",a[i]);
fun(a,n);
}
#include
using namespace std;
int main()
{
bool bo = false;
int m, n;
int temp;
int i, j, sum, t;
while ( cin >> m >> n )
{
for ( i = m; i <= n; ++i )//计算水仙花数
{
sum = 0;
for ( j = i; j; )
{
t = j % 10;
j /= 10;
sum += t * t * t;
}
if ( sum == i )//检查是否与原数相等
{
cout << i << ' ';
bo = true;
}
}
if ( bo == false )//如果没有水仙花数
cout << "no";
}
return 0;
}
#include
using namespace std;
int main()
{
int m,n;
while(cin>>m>>n)
{
int a,b,c,i=0;
while(m<=n)
{
a=m/100;
b=m/10%10;
c=m%10;
if(m==a*a*a+b*b*b+c*c*c)
{
if(i>0)
{
printf(" ");
}
printf("%d",m);
i++;
}
m++;
}
if(i==0)
printf("no");
printf("\n");
}
return 0;
}