# NM2 - by Choice

처음 짰던 코드\
: 아래 코드처럼 선택한 경우와 선택하지 않은 경우 2가지로 나누어서 처리해주었다. 그런데 디버깅을 몇번 해보면 대충 알 수 있는데 1번 경우에서 만든 암호문에 2번 경우 나중에 다시 나올 수 있다!

**그런데 디버깅을 해보면 1번 선택한 경우만 실행시켜줘도 중복을 제외한 모든 오름차순 암호문이 나올 수 있다는 것을 알 수 있다!!!!!!**

예를 들어 1,2,3,4,5,6 6개의 숫자가 있고, 이중 3개를 선택해서 순열을 만든다고 한다면\
1번 경우 : 123, 124,125,126//재귀함수 호출로 이렇게 쭉 만들어지고, 아래 2번 경우 134,135,136 만든 후\
&#x20;                134,135,136//i-for문에서 i가 1증가하여 i+1번째 숫자로 두번째자리 숫자 다시 만듦. 위과정 반복\
&#x20;                 145,146\
&#x20;                 156\
2번 경우 : 134,135,136\
&#x20;                145,146\
&#x20;                156

이런식으로 반복되는 것을 대충 알 수 있다!

```java
public static void go(int index,int selected,String str) {//index : 고른 문자 갯수, selected : input 문자 배열에서 몇번째부터 사용할 것인지,str : 지금까지 선택한 문자열
		//2.정답 찾은 경우 - 조건 검사.
		if(index==L) {//갯수 맞으면 
			if(check(str)) {//조건 맞는지 확인.
				sb.append(str+"\n");
				return;
			}
		}
		//1.정답 될 수 없는 경우.
		if(index>L) return;//지금껏 만든 문자열의 길이가 C보다 크면 그만.
		//3.다음 경우 호출.
		for(int i=selected;i<C;i++) {//C개 문자 중에서 selected번째부터 고른다!
			//1.선택한 경우.
			go(index+1,i+1,str+input[i]);
			//2.선택안한 경우
			go(index,i+1,str+"");
		}
	}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://heunnajo.gitbook.io/algorithms-problem-solving-skills/of/nm2-by-choice.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
