用C语言编写一个n位正整数如果等于它的n个数字的n次方和,该数称为n位自方幂数。设计求3~6位自方幂数。 输出格式要求:"%d位自幂数有:" "%d " "\n" (每位完后换行,每个数字后面跟一个空格)
时间: 2024-02-23 14:01:34 浏览: 98
好的,以下是代码实现:
```c
#include <stdio.h>
int main() {
int i, j, k, m, n;
printf("3位自幂数有:");
for (i = 1; i <= 9; i++) {
for (j = 0; j <= 9; j++) {
for (k = 0; k <= 9; k++) {
m = i * i * i + j * j * j + k * k * k;
if (m == i * 100 + j * 10 + k) {
printf("%d ", m);
}
}
}
}
printf("\n");
printf("4位自幂数有:");
for (i = 1; i <= 9; i++) {
for (j = 0; j <= 9; j++) {
for (k = 0; k <= 9; k++) {
for (n = 0; n <= 9; n++) {
m = i * i * i * i + j * j * j * j + k * k * k * k + n * n * n * n;
if (m == i * 1000 + j * 100 + k * 10 + n) {
printf("%d ", m);
}
}
}
}
}
printf("\n");
printf("5位自幂数有:");
for (i = 1; i <= 9; i++) {
for (j = 0; j <= 9; j++) {
for (k = 0; k <= 9; k++) {
for (n = 0; n <= 9; n++) {
for (int p = 0; p <= 9; p++) {
m = i * i * i * i * i + j * j * j * j * j + k * k * k * k * k + n * n * n * n * n + p * p * p * p * p;
if (m == i * 10000 + j * 1000 + k * 100 + n * 10 + p) {
printf("%d ", m);
}
}
}
}
}
}
printf("\n");
printf("6位自幂数有:");
for (i = 1; i <= 9; i++) {
for (j = 0; j <= 9; j++) {
for (k = 0; k <= 9; k++) {
for (n = 0; n <= 9; n++) {
for (int p = 0; p <= 9; p++) {
for (int q = 0; q <= 9; q++) {
m = i * i * i * i * i * i + j * j * j * j * j * j + k * k * k * k * k * k + n * n * n * n * n * n + p * p * p * p * p * p + q * q * q * q * q * q;
if (m == i * 100000 + j * 10000 + k * 1000 + n * 100 + p * 10 + q) {
printf("%d ", m);
}
}
}
}
}
}
}
printf("\n");
return 0;
}
```
输出结果为:
```
3位自幂数有:153 370 371 407
4位自幂数有:1634 8208 9474
5位自幂数有:54748 92727 93084
6位自幂数有:548834
```
阅读全文