dzungchaos

C++ "VD khử đệ quy"

Jan 15th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.36 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. //long f(int n){
  5. //  if (n == 0) return 1;
  6. //  return 2 * f(n - 1) + 1 - n % 3;
  7. //}
  8.  
  9. long f1(int n){
  10.     int ans = 1;
  11.     for(int i = 1; i <= n; i++){
  12.         ans = ans*2 + 1 - i%3;
  13.     }  
  14.     return ans;
  15. }
  16.  
  17. //long sum(int n) {
  18. //  if (n == 0) return 1;
  19. //  return sum(n-1) + f(n);
  20. //}
  21.  
  22. int main() {
  23.     cout << f1(8) << endl;
  24.        
  25. }
Advertisement
Add Comment
Please, Sign In to add comment