#include <stdio.h> int main() { int i,x,y,z; for(i=100;i<1000;i++) { x = i/100; y = i-x*100; if(y>0) { y= y/10; z =i -x*100 - y*10; if(i == x*x*x + y*y*y + z*z*z) { printf("%d ",i); }
}
} return 0; } 不难吧.
public static void main(String[] args) { for (int i = 100; i < 1000; i++) { int a = i / 100; int b = i / 10 % 10; int c = i % 10; if (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3) == i) { System.out.println(i + "是水仙花数"); } } }
public static void main(String[] args) { int n = 7;//n为水仙花数的位数 int[] x = new int[n]; for (int i = (int) Math.pow(10, n - 1); i < Math.pow(10, n); i++) { long total = 0; for (int j = x.length - 1; j >= 0; --j) { x[j] = i / (int) Math.pow(10, j) % 10; total = (long) (total + Math.pow(x[j], n)); } if (total == i) { System.out.println(i + "是水仙花数"); } } }这个只能算到8,9位的水仙花数, 10多20位的水仙花数用这个不行,哪位写一个10多位的水仙花数的算法出来。
#include <stdio.h>
int main()
{
int i,x,y,z;
for(i=100;i<1000;i++)
{
x = i/100;
y = i-x*100;
if(y>0)
{
y= y/10;
z =i -x*100 - y*10;
if(i == x*x*x + y*y*y + z*z*z)
{
printf("%d ",i);
}
}
}
return 0;
}
不难吧.
{
for (int i = 100; i < 1000; i++)
{
int a = i / 100;
int b = i / 10 % 10;
int c = i % 10;
if (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3) == i)
{
System.out.println(i + "是水仙花数");
}
}
}
public static void main(String[] args)
{
int n = 7;//n为水仙花数的位数
int[] x = new int[n];
for (int i = (int) Math.pow(10, n - 1); i < Math.pow(10, n); i++)
{
long total = 0;
for (int j = x.length - 1; j >= 0; --j)
{
x[j] = i / (int) Math.pow(10, j) % 10;
total = (long) (total + Math.pow(x[j], n));
}
if (total == i)
{
System.out.println(i + "是水仙花数");
}
}
}这个只能算到8,9位的水仙花数,
10多20位的水仙花数用这个不行,哪位写一个10多位的水仙花数的算法出来。
http://blog.csdn.net/Iilovetopview/archive/2009/11/13/4808839.aspx