[C/C++ 演算法]- 阿姆斯壯數
剛才找資料時發現一個C/C++的教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。
拷貝來源:
http://openhome.cc/Gossip/AlgorithmGossip/
http://openhome.cc/Gossip/AlgorithmGossip/ArmstrongNumber.htm
 #include <stdio.h>  #include <stdlib.h> 
  int isNarcissistic(double); void narcissistic(double*, int);
  int main(void) {      double armstrongs[88] = {0};     narcissistic(armstrongs, 3);     int i;     for(i = 0; armstrongs[i] != 0; i++) {         printf("%.0f\n", armstrongs[i]);     }     return 0;  } 
  int isNarcissistic(double number) {     int digits[39] = {0};     double num = number;     int i;     for(i = 0; num != 0; i++) {         digits[i] = (int) num % 10;         num = (int) (num / 10);     }     double sum = 0.0;     int j;     for(j = 0; j <= i; j++) {         sum += pow(digits[j], i);     }     return sum == number; }
  void narcissistic(double* armstrongs , int n) {     double bound = pow(10, n < 40 ? n : 39);     double i;     int j;     for(i = 0, j = 0; i < bound; i++) if(isNarcissistic(i)) {         armstrongs[j] = i; j++;     } } 
  | 
 
 
 
沒有留言:
張貼留言