什么是自幂数
自幂数是指一个 n 位数,它的每个位上的数字的 n 次幂之和等于它本身。(例如:当n为3时,有1^3 + 5^3 + 3^3 = 153,153即是n为3时的 一个自幂数)。
自幂数包括:独身数、水仙花数、四叶玫瑰数、五角星数、六合数、北斗七星数、八仙数、九九重阳数、十全十美数。
什么是水仙花数
水仙花数指的是一个三位数它的每个位上的数字的 3 次幂之和等于它本身。
全局变量和局部变量
全局变量:全局变量定义位置位于函数外部,在整个程序都可以访问。
局部变量:局部变量定义位于函数内部,在函数体内才可以访问
注意:语句块定义的变量只能作用于语句块里面。
自幂数代码实现思路
对于自幂数核心三个就是怎么求出一个数是几位数、怎么求出这个数每一个位上的数,然后单拎出来、怎么把单拎出来的数进行次方计算。我个人的解决思路是,以这个数是几位数为核心,然后再利用switch语句分为1-5位数,判断后进入特定位数的处理(包括每一位的单拎处理和次方处理)。
代码实现
自定义方法求出这个数是几位数
//自定义方法求出这个数是几位数
int Digit(int number){
//计算输入的数字是几位数ß
int count = 0;
do {
number = number / 10;
count++;
} while (number != 0);
return count;
}
自定义方法对某一个数进行次方计算
//计算次方
int powerer(int a, int b) {
int Order = 1;
if (b == 0) {
Order = 1;
}
else {
for (int i = 1; i <= b; i++) {
Order *= a;
}
}
return Order;
}
利用switch语句进行判断进入某一个位数处理
switch (count) {
case 1:
ge = i % 10;
if (i == powerer(ge,count)) {
printf("%d\n",i);
}
break;
case 2:
shi = i / 10 % 10;
ge = i % 10;
if (i == powerer(shi, count) +powerer(ge, count)) {
printf("%d\n",i);
}
break;
case 3:
bai = i / 100 % 10;
shi = i / 10 % 10;
ge = i % 10;
if (i == powerer(bai, count) + powerer(shi , count) +powerer(ge, count)) {
printf("%d\n",i);
}
break;
case 4:
qian = i /1000 % 10;
bai = i / 100 % 10;
shi = i / 10 % 10;
ge = i % 10;
if (i == powerer(qian, count) + powerer(bai, count) + powerer(shi , count) +powerer(ge, count)) {
printf("%d\n",i);
}
break;
case 5:
wan = i /10000 % 10;
qian = i / 1000 % 10;
bai = i / 100 %10;
shi = i / 10 % 10;
ge = i % 10;
if (i == powerer(wan,count) + powerer(qian, count) + powerer(bai, count) + powerer(shi , count) +powerer(ge, count)) {
printf("%d\n",i);
}
break;
}
整体代码可运行
#include <math.h>
#include <stdio.h>
int Digit(int number){
//计算输入的数字是几位数ß
int count = 0;
do {
number = number / 10;
count++;
} while (number != 0);
return count;
}
int powerer(int a, int b) {
int Order = 1;
if (b == 0) {
Order = 1;
}
else {
for (int i = 1; i <= b; i++) {
Order *= a;
}
}
return Order;
}
int main(int argc, const char * argv[]) {
int ge = 0;
int shi = 0;
int bai = 0;
int qian = 0;
int wan = 0;
for(int i = 0;i < 100000;i++){
int count = Digit(i);
//计算输入的数个十百等位等数
switch (count) {
case 1:
ge = i % 10;
if (i == powerer(ge,count)) {
printf("%d\n",i);
}
break;
case 2:
shi = i / 10 % 10;
ge = i % 10;
if (i == powerer(shi, count) +powerer(ge, count)) {
printf("%d\n",i);
}
break;
case 3:
bai = i / 100 % 10;
shi = i / 10 % 10;
ge = i % 10;
if (i == powerer(bai, count) + powerer(shi , count) +powerer(ge, count)) {
printf("%d\n",i);
}
break;
case 4:
qian = i /1000 % 10;
bai = i / 100 % 10;
shi = i / 10 % 10;
ge = i % 10;
if (i == powerer(qian, count) + powerer(bai, count) + powerer(shi , count) +powerer(ge, count)) {
printf("%d\n",i);
}
break;
case 5:
wan = i /10000 % 10;
qian = i / 1000 % 10;
bai = i / 100 %10;
shi = i / 10 % 10;
ge = i % 10;
if (i == powerer(wan,count) + powerer(qian, count) + powerer(bai, count) + powerer(shi , count) +powerer(ge, count)) {
printf("%d\n",i);
}
break;
}
}
return 0;
}
运行结果
0
1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474
54748
92727
93084
Program ended with exit code: 0
个人想法:以上只是个人提供的一种不考虑算法学习思路。