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;
}
'알고리즘' 카테고리의 다른 글
C++ 알고리즘 - 15649 N과 M (1) (백트래킹) (0) | 2025.01.20 |
---|---|
C++ 알고리즘 - 9655 돌 게임 (0) | 2025.01.20 |
C++ 알고리즘 - 1789 수들의 합 (0) | 2025.01.20 |
C++ 알고리즘 - 1475 방 번호 (0) | 2025.01.19 |
C++ 알고리즘 - 4673 셀프 넘버 (0) | 2025.01.19 |