2019년 8월 10일 토요일

nCr 조합 구하기 알고리즘


combination ( nCr ) (배열, 선택체크배열, 현재index, 선택 필요갯수 r, 현재 선택갯수 c) 

만약 선택한 갯수가 r이면 선택완료
   선택한것 출력후 return;
만약 index가 배열의 범위 밖이면 선택 불가능
   return;
현재 index 선택했다고 표기
combination 호출 ( 다음 index에 대하여, 선택 갯수 +1 )
현재 index 선택하지 않았다고 표기
combination 호출 ( 다음 index에 대하여, 선택갯수 그대로 )

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
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone {
    public static void main(String[] ar){
        Ideone ex = new Ideone();
        int[] arr = { 1234 };
        int r = 3;
        int checkCount = 0;
        boolean[] selected = new boolean[arr.length];
        int startIndex = 0;
        combination(arr,selected,0,r,0);
    }
    public static void combination(int[] arr,boolean[] selected, int index ,int r , int checkCount){
        if(checkCount == r){
            for(int i = 0; i < arr.length++i){
                if(selected[i]){
                    System.out.print(arr[i]+" ");
                }
            }
            System.out.println();
            return;
        }
        if(index == arr.length){
            return;
        }
        selected[index] = true;
        combination(arr,selected, index+1, r, checkCount+1);
        selected[index] = false;
        combination(arr,selected, index+1, r, checkCount);
    }
}
cs


nPr 순열 구하기 알고리즘


permutation( nPr ) (배열, 선택 체크 배열, 선택 필요 개수 r, 스택)
만약 스택에 들어있는 값의 갯수가 r이면 ( r개 선택 완료 )
       스택에 있는 값 출력 (bottom부터 top까지 역순으로 출력해야함)
       return;
index <- 배열의 처음( 0 )부터 끝( n-1 )까지 탐색 
   만약 index가 선택되지 않았다면
       index 선택되었다고 표기하고
       index에 해당하는 값을 스택에 넣는다.
           permutation 호출
       index 선택된것 표기 해제
       스택에서 pop (index에 해당하는 값을 꺼냄)
         
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
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone {
    public static void main(String[] ar){
        Ideone ex = new Ideone();
        int[] arr = { 12};
        Stack<Integer> s = new Stack<>();
        int r = 2;
        boolean[] selected = new boolean[arr.length];
        combination(arr,selected,r,s);
    }
    public static void combination(int[] arr,boolean[] selected, int r, Stack<Integer> s){
        if(s.size() == r){
            System.out.println(s);
            return;
        }
        for(int i = 0; i < arr.length++i){
            if(!selected[i]){
                selected[i] = true;
                s.push(arr[i]);
                combination(arr,selected,r,s);
                selected[i] = false;
                s.pop();
            }
        }
    }
}
cs



2019년 4월 8일 월요일

visual studio code error exit code: 3221225785


작업환경 : Win10 64-bit

https://webnautes.tistory.com/1158

위 블로그를 보고 vs code를 설치하고

#include <string>을 사용한 코드를 컴파일까지는 했는데

실행하면 exit code: 3221225785와 함께 비정상 종료.

구글링 결과 MinGW가 32bit 버전용이어서 생긴 문제라고 추측,
(추측입니다. 혹시 정확한 원인 아시는 분은 알려주시면 감사드리겠습니다.)

MinG2 64bit 버전 설치후 실행하니 문제 없음.

64bit 운영체제를 사용중 이라면 설치 도중 옵션에서 i686을 x86_64로 바꿔줍시다.

다운로드 경로 https://sourceforge.net/projects/mingw-w64/