구현(Implementation)
백준 11966번: 2의 제곱인가?
cepiloth
2018. 7. 23. 17:12
반응형
https://www.acmicpc.net/problem/11966
1. 문제
자연수 N이 주어졌을 때, 2의 제곱수면 1을 아니면 0을 출력하는 프로그램을 작성하시오.
2. 알고리즘
키워드 - 구현
3. 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include <iostream> #include <sstream> #include <string> #include <algorithm> #include <functional> #include <vector> #include <list> #include <queue> #include <deque> #include <map> #include <set> #include <stack> #include <math.h> using namespace std; #define MAX_SIZE 100 #define INF 0x7fffffff #define CENDL "\n" #define ll long long ll table[31]; int main() { for (int i=0;i<31; i++) { table[i] = 1 << i; } long long n; cin >> n; int sol = 0; for(int i=0; i<31; i++) { if (table[i] == n) { sol = 1; break; } } if (sol) { cout << 1 << CENDL; } else { cout << 0 << CENDL; } return 0; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <iostream> #include <sstream> #include <string> #include <algorithm> #include <functional> #include <vector> #include <list> #include <queue> #include <deque> #include <map> #include <set> #include <stack> #include <math.h> using namespace std; #define MAX_SIZE 100 #define INF 0x7fffffff #define CENDL "\n" #define ll long long ll table[31]; int main() { int n; cin >> n; int sz = __builtin_popcount(n); if (sz == 1) cout << 1; else cout << 0; return 0; } | cs |
반응형