Skip to content

Instantly share code, notes, and snippets.

@tamanobi
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamanobi/53056d104a8baf156940 to your computer and use it in GitHub Desktop.
Save tamanobi/53056d104a8baf156940 to your computer and use it in GitHub Desktop.
トリボナッチ数列(非再帰型)
#include <iostream>
#include <sstream>
using namespace std;
// tribonacci sequence
long tribonacci(int n){
long a=1, b=2, c=4;
if(1>n){
return 0;
}else if(1==n){
return a;
}else if(2==n){
return b;
}else if(3==n){
return c;
}else{
for(int i=n-3; i>0; i--){
long tmp = a + b + c;
a = b;
b = c;
c = tmp;
}
return c;
}
}
int main(void){
int n = 0;
string str;
getline(cin, str);
istringstream iss(str);
iss >> n;
cout << n << endl;
cout << tribonacci(n) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment