标签:style blog http io ar for strong sp div
学习资源《Algorithms》,作者S.Dasgupta,C.H.Papadimitriou,and U.V.Vazirani。#include "stdafx.h" #define MAX_NUM 50 long fib1(int n) { if (0 == n) return 0; if (1 == n) return 1; return fib1(n-1)+fib1(n-2); } int _tmain(int argc, _TCHAR* argv[]) { while(true) { int input = 0; printf("please input fibonacci number:"); scanf("%d", &input); if (input>MAX_NUM) { printf("please input number less than %d", MAX_NUM); continue; } if (input<0) { break; } int result1 = fib1(input); printf("fib1 result:%d\n", result1); } return 0; }
#include "stdafx.h" #define MAX_NUM 50 long fib2(int n) { long array[MAX_NUM]; array[0] = 0; array[1] = 1; for (int i=2; i<=n; ++i) { array[i] = array[i-1]+array[i-2]; } return array[n]; } int _tmain(int argc, _TCHAR* argv[]) { while(true) { int input = 0; printf("please input fibonacci number:"); scanf("%d", &input); if (input>MAX_NUM) { printf("please input number less than %d", MAX_NUM); continue; } if (input<0) { break; } int result2 = fib2(input); printf("fib2 result:%d\n", result2); } return 0; }
标签:style blog http io ar for strong sp div
原文地址:http://blog.csdn.net/segen_jaa/article/details/40045825