> For the complete documentation index, see [llms.txt](https://heunnajo.gitbook.io/algorithms-problem-solving-skills/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://heunnajo.gitbook.io/algorithms-problem-solving-skills/brute-force/n-m3.md).

# N M3

배열이 생성될 때마다 출력하면 시간 제한(1초) 에 시간 초과가 뜨기 때문에 StringBuilder를 이용하여 SB에 차곡차곡 쌓은다음 **main 함수에서 한번만에 다 출력**한다.

```java
import java.util.*;
public class NM3_SB_2nd {
	static int[] a = new int[10];
	static StringBuilder go(int index,int n,int m) {
		if(index == m) {
			StringBuilder sb = new StringBuilder();
			for(int i=0;i<m;i++) {
				sb.append(a[i]);
				if(i!=m-1) {sb.append(" ");}
			}
			sb.append("\n");
			return sb;
		}
		StringBuilder ans = new StringBuilder();
		for(int i=1;i<=n;i++) {
			a[index] = i;
			ans.append(go(index+1,n,m));
		}
		return ans;
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		System.out.print(go(0,n,m));
	}

}
```

![StringBuilder를 이용한 재귀함수 구현](https://3269900549-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIbwNq54Ge4eqsziHM7%2F-Ma78bPEKIhIWoNbu0rt%2F-Ma7Cfowcxrg0SUKCFDY%2Fimage.png?alt=media\&token=4c29e32d-ac3a-490c-8b76-60fe197c95f4)
