매일프로그래밍 - 코딩테스트 문제 30
Given an integer, count number of 1s in binary representation of an integer.
시간 복잡도: O(log n)
input: 6 // 110
output: 2
input: 13 // 1101
output: 3
----------------------------------------------------------------------------
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 void getCount(int input)
6 {
7 int count = 0;
8
9 fprintf(stdout, "input[%5d]\t[0x%04x]\t", input, input);
10 while (input > 0) {
11 if ( (input % 2) == 1 )
12 count++;
13 input = input/2 ;
14 }
15 fprintf(stdout, "Count[%d]\n",count);
16 }
17
18 int main(int argc, char** argv)
19 {
20 #ifdef __TBD__
21 int loopCnt = 0;
22
23 for (loopCnt = 1; loopCnt < argc; loopCnt++) {
24 getCount(atoi(argv[loopCnt]));
25 }
26 #else
27
28 while (argc > 1) {
29 *argv++;
30 argc--;
31 getCount(atoi(*argv));
32 }
33 #endif
34
35 return 0;
36 }
댓글 없음:
댓글 쓰기