Online Judge/백준
[백준][C++] 1094: 막대기
vince joe
2020. 8. 13. 07:06
문제 링크: https://www.acmicpc.net/problem/1094
입력을 이진수로 바꿨을 때 1의 개수를 세면 됩니다. 즉 popcount를 하면 됩니다. std::bitset
의 count 메소드를 써도 되고, __builtin_popcount
를 써도 되겠죠?
#pragma GCC optimize ("Ofast")
#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int x;
cin >> x;
cout << __builtin_popcount(x) << '\n';
return 0;
}
언제부턴가 msvc에서도 __builtin_popcount
가 써지네요? C++17 설정을 했더니 되는 듯 합니다.
반응형