码迷,mamicode.com
首页 > 其他好文 > 详细

斐波那契数列

时间:2019-04-20 14:32:11      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:斐波那契数   except   std   else   index   pre   i++   ++i   row   

#include<iostream> #include<exception> int fibnaci(int index) { try { if (index >= 0) { int arr[3] = { 1, 1, 0 }; for (int i = 2; i <= index; ++i) { arr[2] = arr[0] + arr[1]; arr[0] = arr[1]; arr[1] = arr[2]; } return index < 2 ? arr[index] : arr[2]; } else { throw std::invalid_argument("index < 0"); } } catch (std::exception &e) { std::cout << "exception:" << e.what() << std::endl; return -1; } } int main() { for (int i = 0; i < 15; i++) { std::cout << fibnaci(i) << "\t"; } std::cout << std::endl; system("pause"); std::cout << fibnaci(-1) << std::endl; system("pause"); return 0; }

斐波那契数列

标签:斐波那契数   except   std   else   index   pre   i++   ++i   row   

原文地址:https://blog.51cto.com/frankniefaquan/2381774

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!