就按照水仙花的定义做就行。
#include
#include
int main(void)
{
for(int i = 1000; i < 10000; i++)
{
int a = i / 1000;
int b = (i - a * 1000) / 100;
int c = (i % 100) / 10;
int d = i % 10;
if(pow(a, 4) + pow(b, 4) + pow(c, 4) + pow(d, 4) == i)
printf("%d\n", i);
}
return 0;
}
其中pow函数返回的double值可以直接和i做比较,因为本身就没有小数的问题。
#include "stdio.h"
int main(int argc,char *argv[]){
int n,t;
for(n=1000;n<10000;n++)
if((t=n%10)*t*t*t+(t=n/10%10)*t*t*t+(t=n/100%10)*t*t*t+(t=n/1000)*t*t*t==n)
printf("%5d",n);
printf("\n");
return 0;
}
运行结果:
#include
using namespace std;
int main()
{
int num = 1000;
do
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
a = num % 10;
b = num / 10 % 10;
c = num / 100 % 10;
d = num / 1000;
if (a * a * a * a + b * b * b * b + c * c * c * c + d * d * d * d == num)
{
cout << num << endl;
}
num++;
} while (num < 10000);
system("pause");
return 0;
}
五位数的:#include
using namespace std;
int main()
{
int num = 10000;
do
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
a = num % 10;
b = num / 10 % 10;
c = num / 100 % 10;
d = num / 1000 % 10;
e = num / 10000;
if (a * a * a * a *a+ b * b * b * b*b + c * c * c * c*c + d * d * d * d*d+e*e*e*e*e == num)
{
cout << num << endl;
}
num++;
} while (num < 100000);
system("pause");
return 0;
}
有规律的