문제
두 정수 N,K를 입력받아 K개의 1을 가진 N자리 이진수들을 출력하시오
입력
두 정수 N, K를 입력( 0 < N <=30, 0 <= K < 8, K <= N )
출력
내림차순으로 출력
예제 입력 예제 출력
2 1 10
01
---------------------------------------------------------------------------------------------------------------------------------------------
2 0 00
---------------------------------------------------------------------------------------------------------------------------------------------
4 2 1100
1010
1001
0110
0101
0011
---------------------------------------------------------------------------------------------------------------------------------------------
소스코드
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 | #include <stdio.h> int binary[30]={0,}; void print(int N,int cnt,int index); int main(void) { int N,cnt; scanf("%d %d",&N,&cnt); print(N,cnt,0); return 0; } void print(int N,int cnt,int index){ if(N==index){ if(cnt!=0) return; for(int i = 0 ; i <N ; i++){ printf("%d",binary[i]); } printf("\n"); return; } if(cnt<0) return; for(int i =1 ; i >=0 ; i--){ binary[index] = i; if(i==1) print(N,cnt-1,index+1); else print(N,cnt,index+1); } } | cs |
'IT Study > Algorithm' 카테고리의 다른 글
NN단 (binarySearch 문제) (0) | 2018.08.29 |
---|---|
[백준 알고리즘]1011번 Fly me to the Alpha Centauri (0) | 2018.08.21 |
[백준 알고리즘]2292번 벌집 (0) | 2018.08.21 |
[백준 알고리즘]4880번 다음수 (0) | 2018.08.21 |
암호화와 복호화 (0) | 2018.05.17 |