Last active
August 29, 2015 14:14
-
-
Save tamanobi/53056d104a8baf156940 to your computer and use it in GitHub Desktop.
トリボナッチ数列(非再帰型)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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