HDU2013 蟠桃记
迭代
#include <iostream>
using namespace std;
int main()
{
int n;
while(cin >> n)
{
int today = 1;
int yesterday = 0;
for(int i=1; i<n; i++)
{
yesterday = (today+1)*2;
today = yesterday;
}
cout << yesterday << endl;
}
return 0;
}
递归版本
#include <iostream>
using namespace std;
int sum (int n)
{
if(n == 1)
{
return 1;
}
else
{
return (sum(n-1)+1)*2;
}
}
int main()
{
int n;
while(cin >> n)
{
cout << sum(n) << endl;
}
return 0;
}