알고리즘

C++ 알고리즘 - 1049 막대기

마루설아 2025. 1. 20. 18:37

https://www.acmicpc.net/problem/1094

 

#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

/******** 함    수 ********/


int main(void) {
	/******** C++ INIT ********/
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);


	/******** 변    수 ********/
	int input;
	int cnt = 0;
	vector<int> v;


	/******** 구    현 ********/
	cin >> input;

	// 입력된 10진수로 2진수 구하기
	while (input > 1) {
		v.push_back(input % 2);
		input /= 2;
	}

	// 마지막 자리수 삽입
	v.push_back(input);

	// 2진수로 변환된 수 중 1의 갯수 카운트
	for (int i = 0; i < v.size(); i++) {
		if (v[i] == 1) cnt++;
	}

	cout << cnt;
}