标签:str dig printf sar 获取 for pre 函数 std
// 1234 = 1^4 + 2^4 + 3^4 + 4^4; // 341 = 3^3 + 4^3 + 1^3 // 类似于水仙花数 #include <stdio.h> #include <math.h> int digitCount(int n) { //此函数用于获取整数的位数 int i = 0; do { n = n / 10; i++; } while ( n > 0 ); return i; } int isArmstrong(int n) { int digitNum = digitCount(n); int sum = 0; int temp = n; while (n > 0) { sum += pow(n % 10, digitNum); n = n / 10; } //n 已经不再是那个n return sum == temp; } int main() { int i; for ( i = 1; i < 5000; ++i ) { if (isArmstrong(i)) { printf("%d is Armstrong number\n", i); } } return 0; }
标签:str dig printf sar 获取 for pre 函数 std
原文地址:https://www.cnblogs.com/profesor/p/13278138.html