1 solutions

  • -3
    @ 2024-3-20 13:21:20

    题意

    将1个整数拆分成n个2x2^x

    思路

    具题目可得,若n为奇数是无法拆分的。所以输出-1。对于偶数,拆就完了。

    代码

    #include<iostream>
    using namespace std;
    int main(){
    	int n;
    	cin>>n;
    	if(n%2==1){
    		cout<<-1;
    		return 0;
    	}
    	while(n!=0){
    		int a=1;
    		while(a<=n)
    			a*=2;//找大于
    		a/=2;
    		cout<<a<<' ';//输出
    		n-=a;
    	}
    }
    
  • 1

Information

ID
6252
Time
1000ms
Memory
256MiB
Difficulty
1
Tags
# Submissions
51
Accepted
26
Uploaded By