写一个函数,判断一个正整数是否是水仙花数(扩展n位的自幂数)

时间:2022-01-01 15:12:16

//参数number是待判断的正整数,n为正整数位数(3位则为水仙花数),满足条件则返回1,反之返回0

bool isZiMiShu(int number, int n) {

    int __num = number, a[n], sum = 0, i = 0;

    while (number > 0) {

        a[i] = number % 10;

        sum += pow(a[i], n);

        number = (number - number % 10) / 10;

        i++;

    }

return (__num == sum && __num/pow(10, n-1) > 1);

}