杭电ACM2013:蟠桃记

来源:互联网 发布:网络直播课堂 编辑:程序博客网 时间:2024/05/22 11:51

递归,公式:f(n)=(f(n-1)+1)*2,n>2;f(2)=4;

#include <iostream>using namespace std;int Fun(int a){    int f, n = a;    if (n == 2) f = 4;    else f = (Fun(n-1) + 1) * 2;    return f;}int main(){    int n,S;    while (cin >> n){        S = Fun(n);        cout << S << endl;    }}

想复杂了,完全可以不用递归的- -!

#include <iostream>using namespace std;int main(){    int n,S=1;    while (cin >> n){        while (n >= 2){            S = (S + 1) * 2;            n--;        }        cout << S << endl;        S = 1;    }}
0 0