闲来无聊,出个超简单的考题给大家玩玩
要求:打印fibonacci数列,数字之间用“,”间隔,但最后一个数字用“.”。直接给出for那段,或思路都行
#include <stdio.h> int fibonacci(int n) // n[0,47) { static int buffer[47] = { 0, 1, 1 }; static int index = 2; if (n > index) { for (++index; index <= n; ++index) buffer[index] = buffer[index-2] + buffer[index-1]; --index; } return buffer[n]; } int main(void) { int i; printf("%d", fibonacci(0)); for (i = 1; i < 47; ++i) printf(", %d", fibonacci(i)); putchar('.'); return 0; }
#include "stdio.h" int main (void){ int i, d2, d1, num, fibs [200]; /*倒2,倒1,几个,fibonacci*/ d2 = fibs [0] = 1; d1 = fibs [1] = 1; printf ("Please input number of fibnaccis\n"); scanf ("%d", &num); fflush (stdin); for (i = 2; i < num; i += 1){ fibs [i] = d2 + d1; d2 = fibs [i - 1]; d1 = fibs [i]; } for (i =0; i < num; i += 1){ printf ("%d", fibs [i]); if (i == num -1) break; printf (", "); } getch (); return 0; }贴下兰州的,简单易懂,"lz1091914999"童鞋的看得我是真费劲啊.....