알고리즘/BOJ

백준 3460번 이진수

꾸준함. 2021. 5. 14. 18:05

문제 링크입니다: https://www.acmicpc.net/problem/3460

 

3460번: 이진수

양의 정수 n이 주어졌을 때, 이를 이진수로 나타냈을 때 1의 위치를 모두 찾는 프로그램을 작성하시오. 최하위 비트(least significant bit, lsb)의 위치는 0이다.

www.acmicpc.net

간단한 구현 문제였습니다.

 

#include <iostream>
using namespace std;
void printBitLocations(int n)
{
int idx = 0;
while (n)
{
if (n % 2)
{
cout << idx << " ";
}
n /= 2;
idx++;
}
cout << "\n";
}
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
for (int t = 0;t < T; t++)
{
int n;
cin >> n;
printBitLocations(n);
}
return 0;
}
view raw .cpp hosted with ❤ by GitHub

 

개발환경:Visual Studio 2017

 

지적, 조언, 질문 환영입니다! 댓글 남겨주세요~

반응형

'알고리즘 > BOJ' 카테고리의 다른 글

백준 3507번 Automated Telephone Exchange  (0) 2021.05.15
백준 3486번 Adding Reversed Numbers  (0) 2021.05.15
백준 3009번 네 번째 점  (0) 2021.05.14
백준 3028번 창영마을  (0) 2021.05.14
백준 3029번 경고  (0) 2021.05.14