计算四位数每一位数字的四次方之和,然后再判断是否等于这个四位数即可。
补充如下:
#include <iostream>
#include <fstream>
using namespace std;
class DIF{
int low,high;
int a[100];
int count;
public :
DIF(int lw=1000,int hi=9999){
low=lw;
high=hi;
}
int isdiff(int x){
int num=x,single;
long sum=0;
while(num!=0) {
single=num%10;
sum+= single*single*single*single;
num/=10;
}
if(sum==((long)x)){
return 1;
}else{
return 0;
}
}
void process(){
count=0;
for(int x=low;x<=high;x++){
if(isdiff(x)){
a[count++]=x;
}
}
}
void show(){
for(int i=0;i<count;i++){
cout<<a[i]<<'\t';
}
cout<<'\n';
cout<<"count="<<count;
ofstream outf("bc02.in");
for(int i=0;i<count;i++){
outf<<a[i]<<'\t';
}
outf<<'\n';
outf<<"count="<<count;
outf.close();
}
};
int main(void){
DIF v(1111,9999);
v.process();
v.show();
}
效果
代码
#include <iostream>
int main()
{
for (int i = 1; i <= 9; ++i) { // 千位数字从1到9枚举
for (int j = 0; j <= 9; ++j) { // 百位数字从0到9枚举
for (int k = 0; k <= 9; ++k) { // 十位数字从0到9枚举
for (int l = 0; l <= 9; ++l) { // 个位数字从0到9枚举
int x = i * 1000 + j * 100 + k * 10 + l;
int sum = i*i*i*i + j*j*j*j + k*k*k*k + l*l*l*l;
if (x == sum) {
std::cout << x << " ";
}
}
}
}
}
return 0;
}
暴力解决