7 solutions

  • 2
    @ 2022-6-12 10:17:30
    #include<bits/stdc++.h>
    using namespace std;
    long long q,w,e,r,t,y,u,i,o,p,s,d,f,g,h,j,k,l,m,n,v,x,z,kk;
    int b[1000];
    int a[1000];
    int c[1000];
    int main()
    {
    	cin>>n;
    	if(n%2==1)
    	{
    		cout<<"-1";
    		return 0;
    	}
    	i=1;x=2;
    	while(n!=0)
    	{
    		if(x*2<=n)
    		x*=2;
    		if(x*2>n)
    		{
    			n-=x;
    			cout<<x<<" ";
    			x=2;
    		}
    		
    		
    		i++;
    	}
    	return 0;
    }
    
    • 1
      @ 2023-9-18 11:54:01

      地球人看完题目首先想到的方法:

      观察题目的样例,for example: 6=22+21=4+26=2^2+2^1=4+2

      当输入的n=6n=6时,22<n2^2<n23>n2^3>n,故记下答案4

      此时n=64=2n=6-4=2

      n=2n=2时,21n2^1≤n22>n2^2>n,故记下答案2

      此时n=0n=0

      输出答案4 2

      也就是说,对于1<n1<n,我们需要找到2kn2^k≤n2k+1>n2^{k+1}>n,记下答案2k2^k

      然后n-=k,继续循环.

      #include<bits/stdc++.h>
      using namespace std;
      int n;
      vector<int>res; 
      int main(){
      	cin>>n;
      	while(n>1){
      		int maxi=1;
      		while(maxi*2<=n){
      			maxi*=2;
      		}
      		n-=maxi;
      		res.push_back(maxi);
      	}if(n)cout<<-1;
      	else{
      		for(auto i:res)cout<<i<<" ";
      		cout<<endl; 
      	}
      	return 0;
      }
      

      时间复杂度:O(log2n)O(log_2n)

      • 1
        @ 2023-8-18 11:24:38
        # include <bits/stdc++.h>
        using namespace std;
        int a[27] = {1};
        int main() {
            int n;
            scanf("%d", &n);
            if (n % 2 == 1 || n == 2) {
                printf("-1\n");
            } else {
                for (int i = 1; i <= 26; i++) {
                    a[i] = a[i - 1] * 2;
                }
                for (int i = 26; i >= 1; i--) {
                    if (n >= a[i]) {
                        n -= a[i];
                        printf("%d ", a[i]);
                    }
                }
            }
            return 0;
        }
        
        • 1
          @ 2023-6-18 15:13:38
          #include <iostream>
          using namespace std;
          int n;
          long long a[30];
          int main()
          {
          	cin>>n;
          	if(n%2==1)
          	{
          		cout<<"-1";
          		return 0;
          	}
          	a[1]=1;
          	for(int i=2; i<=26; i++)
          		a[i]=a[i-1]*2;
          	for(int i=26; i>=2; i--)
          	{
          		if(n>=a[i])
          		{
          			n-=a[i];
          			cout<<a[i]<<' ';
          		}
          		if(n==0)return 0;
          	}
          	return 0;
          }
          
          • 0
            @ 2023-10-20 22:42:48
            #include <bits/stdc++.h>
            using namespace std;
            int main()
            {
            	int n;
            	int a[101];
            	cin>>n;
            	if (n % 2 == 1){cout<<-1;return 0;}
            	int t=1;
            	int k=0;
            	while (n!=0)
            	{
            		if (n%2!=0)
            		{
            			k++;
            			a[k] = t;
            		}
            		t=t*2;
            		n=n/2;
            	}
            	for (int i = k;i >= 1;i--)
            		cout<<a[i]<<" ";
            	return 0;
            }
            
            • 0
              @ 2023-8-11 0:00:54
              #include<bits/stdc++.h>
              using namespace std;
              int poww[35]={1};
              int main()
              {
                  int n,t;
              	cin>>n;
              	if (n%2==1)
              	{
              		cout<<-1;
              		return 0;
              	}
              	for (int i=1;i<=30;i++)
              	{
              		poww[i]=poww[i-1]*2;
              		if (poww[i]>n)
              		{
              			t=i-1;
              			break;
              		}
              	}
                  for (int i=t;i>=1;i--)
              	{
              		while (poww[i]<=n)
              		{
              			cout<<poww[i]<<" ";
              			n-=poww[i];
              			if (n==1)
              			{
              				return 0;
              			}
              		}
              	}
                  return 0;
              }
              
              • -2
                @ 2021-8-28 13:54:29

                思路是找小于 nn 的最大 2i2^i,更新 nn 即可

                #include <bits/stdc++.h>
                using namespace std;
                
                int main() {
                    int a;
                    cin >> a;
                
                    if (a % 2)
                        cout << "-1";
                    else {
                        for (int i = 1; i < 30; i++) {
                            int b = pow(2, i);
                            if (b > a) {
                                cout << int(pow(2, i - 1)) << " ";
                                a = a - pow(2, i - 1);
                                i = 0;
                            }
                            if (a == 0) {
                                return 0;
                            }
                        }
                    }
                    return 0;
                }
                
                • 1

                Information

                ID
                121
                Time
                1000ms
                Memory
                256MiB
                Difficulty
                3
                Tags
                # Submissions
                100
                Accepted
                51
                Uploaded By