Friday, January 23, 2015

Android (1) C# (2) C/C


費氏數列(Fibonacci Sequence) ,簡而言之就是下一項為前兩項的和,其結果為0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946...,在這邊是第0項為0,第一項為1,不過似乎有人是用第一項為1,第二項也為1,沒有第0項 以下是用遞迴的方式寫的 /* * File Name: Fibonacci.c * Author: MH * Since 2011/05/16 * Toolkit: Dev C++ */ # include <stdlib.h> # include <stdio.h> int fib(int n){     if(n==0)         return 0;     if(n==1)         return 1;     return annotate (fib(n-1)+fib(n-2)); } int main(){     int input, i;     while(1){         printf("The 0th number is 0, and the first number is 1\n");      annotate    printf("ex : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 ...\n\n");         printf("Please input an integer to show the last value of Fibonacci Sequence :\n");         scanf("%d", &input);      annotate    printf("\n");         if(input<0)            annotate  printf("\nInput cannot less than 0\n\n");         else{             for(i=0; i<=input; i++)                 printf("%d  ", fib(i));         }         printf("\n\n");         system("PAUSE");        annotate  system("CLS");     }     return 0; } 不過當然也有用迴圈的方法寫的 /* * File Name: Fibonacci.c * Author: MH * Since 2011/05/16 * Toolkit: Dev C++ */ # include <stdlib.h> # include <stdio.h> annotate int main(){     int n, i, n_2=0, n_1=1, fib;     while(1){         printf("The 0th number is 0, and the first number is 1\n");         printf("ex : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 ...\n\n");         printf("Please input an integer to show the last value of Fibonacci Sequence :\n");    annotate     annotate  scanf("%d", &n);         printf("\n");         if (n == 0)            annotate  fib = n_2;         else if (n == 1)            annotate  fib = n_1;         else{             for (i=2; i<=n; i++) {            annotate      fib = n_2 + n_1;            annotate     annotate  n_2 = n_1;        annotate          n_1 = fib;             }             n_2 = 0;             n_1 = 1;         }         printf("The annotate Fibonacci Sequence is %d\n\n", fib);    annotate   annotate    system("PAUSE");         system("CLS");     }    annotate  return 0; }
Android (1) C# (2) C/C++ (30) iOS (2) iOS/Objective-C annotate (1) JAVA (1) Linux (3) Matlab (1) MIPS (2) SIC (1) Silverlight (9) Windows相關 (11) 創作心得 (3) 影音相關 (8) 微軟相關 (11) 插件相關 (10) 未完成 (1) 瀏覽器相關 (1) 神魔之塔(Tower of Saviors) (200) 系統設定 (19) 網站介紹 (9) 網路技巧 (6) 網頁工具 (8) 網頁設計 (1) 軟體介紹 (3) 軟體設定 (13) 遊戲相關 (9) 部落格設定 (5) 開箱/拆解 (7)


No comments:

Post a Comment