#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nbool isPowerOfTwo(int x) {\n return (x & (x - 1)) == 0;\n}\n\nvector<int> excellentSplit(int n) {\n vector<int> res;\n for (int i = n; i >= 1; i--) {\n if (isPowerOfTwo(i)) {\n res.push_back(i);\n n -= i;\n if (n == 0) {\n return res;\n }\n }\n }\n return vector<int>();\n}\n\nint main() {\n int n;\n cin >> n;\n \n vector<int> res = excellentSplit(n);\n if (!res.empty()) {\n for (int i = 0; i < res.size(); i++) {\n cout << res[i] << " ";\n }\n } else {\n cout << -1;\n }\n \n return 0;\n}

判断正整数的优秀拆分 - 算法题解

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

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