#include #include #include using namespace std;

string powOf2(int n) { if (n == 0) return '2(0)'; if (n == 1) return '2'; string res = ''; vector powers; int cnt = 0; while (n > 0) { if (n % 2 == 1) { powers.push_back(powOf2(cnt)); } cnt++; n /= 2; } for (int i = powers.size()-1; i >= 0; i--) { if (i != powers.size()-1) res += '+'; res += powers[i]; } return res; }

int main() { int n; cin >> n; cout << powOf2(n) << endl; return 0; }

C++ 实现正整数的 2 进制表示 (二进制幂次方表示)

原文地址: https://www.cveoy.top/t/topic/o7IA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录