【 preface 】
Today is the third day of Li Kou punch in 19 day !
Too busy , It's a little late , Sorry, huh , Recently, the author has a little more homework , There are also several better questions , So I sorted it out .
subject : Find the number of daffodils
Title Description :
reach 0~100000 All between “ Narcissistic number ” And output .
“ Narcissistic number ” Is a n digit , Of its figures n The sum of the powers is exactly equal to the number itself , as : 153=1 ^ 3+5 ^ 3+3 ^ 3, be 153 It's a “ Narcissistic number ”.
Pay attention , Normally, we count daffodils in three digits , But this question is not , This question should be calculated to 100000 .
Problem solution :
The topic is about one n digit , Of its figures n The sum of the powers is exactly equal to the number itself , So we need to know the number of digits required , And on every digit of this number n The power also needs to be calculated .
first , Find out how many digits a number has
// judge num Number of digits // Normal code int Dig(int num) { int count = 0; while (num) { count++; num
= num / 10; } return count; } // Recursive solution // Recursive solution int Dig(int num) { int count = 0;
// Find boundary if (num < 10) { return 1; } else { count = Dig(num / 10); return ++count;
} }
secondly , calculation n of k Power
// seek n of k Power // Common method int Pow(int n, int k) { int ret = 1; // Judge special situations if (n == 0) {
return 0; } while (k) { ret = ret * n; k--; } return ret; } // Recursive solution int Pow(int
n, int k) { // Find boundary if (k == 0) { return 1; } else { return n * Pow(n, k - 1); } }
last , Determine whether it is the number of daffodils
// Determine whether it is the number of daffodils int func(int num) { int sum = 0; int number = num; int n =
Dig(num); int i = 0; for (i = 0; i < n; i++) { sum = sum + Pow(number % 10, n);
number = number / 10; } if (sum == num) { return 1; } else { return 0; } }
Complete code :
// reach 0~100000 All between “ Narcissistic number ” And output . //“ Narcissistic number ” Is a n digit , Of its figures n The sum of the powers is exactly equal to the number itself , as : 153=1 ^
3+5 ^ 3+3 ^ 3, be 153 It's a “ Narcissistic number ”. #include<stdio.h> // seek n of k Power // Common method //int Pow(int n,
int k) //{ // int ret = 1; // // Judge special situations // if (n == 0) // { // return 0; // } //
while (k) // { // ret = ret * n; // k--; // } // return ret; //} // Recursive solution : int
Pow(int n, int k) { // Find boundary if (k == 0) { return 1; } else { return n * Pow(n, k
- 1); } } judge num Number of digits //int Dig(int num) //{ // int count = 0; // while (num) // {
// count++; // num = num / 10; // } // return count; //} // // Recursive solution int Dig(int
num) { int count = 0; // Find boundary if (num < 10) { return 1; } else { count = Dig(num
/ 10); return ++count; } } // Determine whether it is the number of daffodils int func(int num) { int sum = 0; int
number = num; int n = Dig(num); int i = 0; for (i = 0; i < n; i++) { sum = sum
+ Pow(number % 10, n); number = number / 10; } if (sum == num) { return 1; }
else { return 0; } } int main() { int i = 0; for (i = 0; i <= 100000; i++) { if
(1 == func(i)) { printf("%d\n", i); } } return 0; }
summary
Today is the third day of Li Kou punch in 19 day !
I was too busy yesterday , I didn't have time to send it , This article is a supplement to yesterday's , Sorry, sorry, ha .
Technology