CSP-J2020第一题(优秀的拆分)题解

题目
题目传送门
解题思路
因为题目中说不含有2的0次方,所以奇数要直接输出-1。若n为偶数,就每次减掉在n>=2的m次方的情况下最大的2的m次方,并且输出m。
参考代码

#include <iostream>
#include <cstdio>
using namespace std;
int main() {
    
    
	freopen("power.in", "r", stdin);
	freopen("power.out", "w", stdout);
 	int n, x = 1 << 24;
 	cin >> n;
 	if (n % 2) cout << -1 << endl;
 	else {
    
    
 		while (n) {
    
    
 			if (n >= x) {
    
    
 				n -= x;
 				cout << x << " ";
 			}
 			x /= 2;
 		}
 		cout << endl;
 	}
 	return 0;
}

猜你喜欢

转载自blog.csdn.net/yueyuedog/article/details/112462222
今日推荐