本帖最后由 jhq999 于 2022-9-12 21:46 编辑 int unsignedintpow(int x,int y)
{
int s=x;
while(--y)s*=x;
return s;
}
int main() {
int N, i, j, k, sum = 0, y;
printf("输入一个正整数(3<=N<=7):");
scanf("%d", &N);
for (i = unsignedintpow(10, N - 1); i < unsignedintpow(10, N); i++) {
int x = i;
for (j = 0; j < N; j++) {
y = x % 10;
x = x / 10;
sum = sum + unsignedintpow(y, N);
}
if (i == sum) {
printf("%d\n", sum);
}
sum = 0;
}
return 0;
}
scanf("%d", &N);
min= unsignedintpow(10, N - 1),max=unsignedintpow(10, N);
int numNpow[10];
for(i=0;i<10;i+=1){
numNpow[i]=unsignedintpow(i, N);
}
for (i=min; i <max ; i++) {
int x = i;
for (j = 0; j < N&&sum<max; j++) {
y = x % 10;
x = x / 10;
sum = sum + numNpow[y];
}
if (i == sum)
printf("%d\n", sum);
sum = 0;
}
unsigned long long min,max;
unsigned long long unsignedintpow(unsigned long long x,unsigned long long y)
{
unsigned long long s=x;
while(--y)s*=x;
return s;
}
unsigned long long pownumadd(unsigned long long *numpow,unsigned long long sum,unsigned long long num,unsigned long long n)
{
if(0==n)
{
if(sum==num&&sum>=min)printf("%llu\n",sum);
return 1;
}
else
{
for(unsigned long long i=num?0:1; i<10; i+=1)
{
if(sum+numpow[i]<max)
{
pownumadd(numpow,sum+numpow[i],num*10+i,n-1);
}
else
break;
}
}
return 0;
}
int main() {
unsigned long long N,i;
printf("输入一个正整数(3<=N<=7):");
scanf("%llu", &N);
min= unsignedintpow(10, N - 1),max=unsignedintpow(10, N);
unsigned long long numNpow[10];
for(i=0;i<10;i+=1){
numNpow[i]=unsignedintpow(i, N);
}
pownumadd(numNpow,0,0,N);
return 0;
}
|