有一个函数的定义如下
⎪
⎪
⎪
⎪
⎨
⎪
⎪
⎪
⎪
⎧
0
1
3f(
2
x
)−1
3f(
2
x+1
)−1
x≤0
x=1
x>1 and x%2=0
x>1 and x%2=1
其中 xx 为整数。
输入格式
输入一个整数 x(-10^5 \le x \le 10^5)x(−105≤x≤105)。
输出格式
输出函数 f(x)f(x) 值。
格式说明
输出时每行末尾的多余空格,不影响答案正确性
样例输入1
3
样例输出1
5
样例输入2
10
样例输出2
41
#include <iostream>
using namespace std;
int f(int x){
if(x<=0){
return 0;
}
else{
if(x==1){
return 1;
}
else{
if(x%2==0){
return 3*f(x/2)-1;
}
else{
return 3*f((x+1)/2)-1;
}
}
}
}
int a;
int main() {
cin>>a;
cout<<f(a);
return 0;
}