백준 algorithm
백준 1094 - 막대기
cosmohoo
2020. 8. 9. 19:42
반응형

=> 간단한 구현 문제입니다.
=> 입력받은 X의 값이 bar의 값을 뺌에 따라 0이 될 때까지 빼주면 됩니다.
=> if문을 이용해서 간단히 해결할 수 있습니다.
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int N;
cin >> N;
int cnt=0;
int bar=64;
while(N > 0)
{
if( bar <= N)
{
N -= bar;
cnt++;
}
else
{
bar /=2;
}
}
cout<<cnt<<'\n';
return 0;
}


반응형