寻找n位的水仙花数:例如,655=6*55+65*5;1461=1*461+14*61+146*1
要求:输入位数n,输出水仙花数
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int mi(int x) {
int i = 0,k=1;
for (i = 0; i < x-1; i++) {
k *= 10;
}
return k;
}//求出n位数范围
int main()
{
int n = 0;
scanf("%d", &n);
int min = mi(n);
int max = min * 10-1;//确定最大最小值
int s = min;
for (s = min; s <= max; s++) {
int mod = 0, ret = 0, a = s,sum=0;
int i = 0;
for (i = 1; i <= n; i++) {
mod = a / mi(i);//过程相当于对1,10,100,1000……取模取余并相乘
ret = a % mi(i);
sum += mod * ret;
}
if (sum == s) {//判断
printf("%d ", s);//输出
}
}
return 0;
}