Whether a number can be represented by the sum of 3 powers of 2

topic

#include<bits/stdc++.h>
using namespace std;
int n,num,ans;
int main()
{
    
    
	scanf("%d", &n);
	num = n;
	while(n) ans += n&1, n >>= 1;
	if(ans > 3 || num & 1 || num < 6) cout << "NO";//2的个数大于3 没和合并
	else cout << "YES";//2的个数小于3   可以拆出来
	return 0;
}

In fact, really want to approach
the number of enumeration little violence

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	int n;
	cin>>n;
	for(int i=1;i<=32;i++){
    
    
		for(int j=1;j<=32;j++){
    
    
			for(int h=1;h<=32;h++){
    
    
				if(pow(2,i)+pow(2,j)+pow(2,h)==n){
    
    
					cout<<"YES";
					return 0;
				}
			}
		}
	}
	cout<<"NO";
	return 0;
}

Guess you like

Origin blog.csdn.net/cosx_/article/details/110246892