前言
简单问题,写的不好,大佬见谅.
如有疏漏,欢迎指正,小编垂首.
问题描述:
实现代码:
代码如下(示例):
#include<iostream>
using namespace std;
int result(int x) {
if(x <= 0) return 0;
if(x == 1) return 1;
if (x > 1) {
if (x % 2 == 0) return 3 * result(x / 2) - 1;
if (x % 2 == 1)return 3 * result((x + 1) / 2) - 1;
}
}
int main() {
int x;
cin >> x;
cout << result(x) << endl;
}
测试案例:
总结
分段函数,使用if语句即可