0分求正解
查看原帖
0分求正解
1200191
封禁用户楼主2024/10/2 18:08
#include <bits/stdc++.h>
using namespace std;

string to_b(int n) {
    string b = "";
    while (n > 0) {
        b = (n % 2 == 0 ? "0" : "1") + b;
        n /= 2;
    }
    return b;
}

string power(int exp) {
    if (exp == 0) return "0";
    if (exp == 1) return "2";
    string result = "2(";
    string sub = power(exp / 2);
    if (exp % 2 == 0) {
        result += sub + ")";
    } else {
        result += sub + ")+2(" + sub + ")";
    }
    return result;
}

string ex(int n) {
    string b = to_b(n);
    string result = "";
    for (int i = 0; i < b.length(); ++i) {
        if (b[i] == '1') {
            string exp = to_string(b.length() - 1 - i);
            string cexp = power(stoi(exp));
            result += "2(" + cexp + ")";
            if (i != b.length() - 1) {
                result += "+";
            }
        }
    }
    return result;
}
int n;
int main() {
    cin >> n;
    string result = ex(n);
    cout << result << "\n";
    return 0;
}
2024/10/2 18:08
加载中...