P7071 [CSP-J2020] 优秀的拆分 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include<bits/stdc++.h>
using namespace std;
int a[100];
int x;
int main()
{
int n;
scanf("%d",&n);
//如果是奇数,直接输出-1
if(n&1) cout<< -1; //n&1:与操作,判断 n 二进制最右一位是否为 1
else
{
//将n转换为二进制数
while(n)
{
a[x++]=n%2;
n=n>>1; //n>>1:移位操作,删除n二进制的最右一位。等价于 n = n /2;
}
for(int i = x - 1; i >= 0; i--)
{
if(a[i])
{
int y = pow(2,i);
cout << y << ' ';
}
}
}
return 0;
}