문제
코드1
#include <bits/stdc++.h>
using namespace std;
int n;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
cout << bitset<8>(n).count() << '\n';
return 0;
}
코드2
#include <bits/stdc++.h>
using namespace std;
int x, sum, barLength = 64, cnt;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> x;
while (x != sum)
{
if ((sum + barLength) <= x)
{
sum += barLength;
cnt++;
}
barLength /= 2;
}
cout << cnt << '\n';
return 0;
}
*문제 아이디어
x = 23 이면m 16, 4, 2, 1 인 막대기를 더해서 만들 수 있다.
즉 23 = 00010111 이고 1 개수만 카운트해주면 된다.
'개발 > 알고리즘' 카테고리의 다른 글
프로그래머스 폰켓몬 javaScript (0) | 2021.05.07 |
---|---|
프로그래머스 실패율 javaScript (0) | 2021.05.07 |
백준 1987 알파벳 C++ (0) | 2021.05.03 |
백준 17298 오큰수 C++ (Stack) (0) | 2021.05.03 |
백준 17070 파이프옮기기1 C++ (0) | 2021.04.18 |