X. Convert To Decimal 2 – Codeforces Problem Solution in C++

Disclaimer: Make sure you tried enough before checking the solution

Problem: https://codeforces.com/group/MWSDmqGsZm/contest/219432/problem/X

#include<bits/stdc++.h>
using namespace std;
int main(){
	int numberTest;
	cin>>numberTest; // 3    0 1 2
	long long num;
	for(int i=0;i<numberTest;i++){
		cin>>num;
		int ones = 0;
		while(num > 0){
			if(num % 2 == 1){
				ones++;
			}
			num /=2;
		}
		long long sum = 0;
		for(int z=0;z<ones;z++){
			sum+= 1 * pow(2,z); //
		}
		cout<<sum<<endl;
	}
	
}

Assiut University Training – Newcomers solution

Leave a Comment