ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

15652๋ฒˆ: N๊ณผ M (4)

ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ˆ˜์—ด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถœ๋ ฅํ•˜๋ฉด ์•ˆ๋˜๋ฉฐ, ๊ฐ ์ˆ˜์—ด์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ์ˆ˜์—ด์€ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•ด

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	var n, m int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanln(reader, &n, &m)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var result = make([]int, m+1)
	sequence(writer, result, 0, n, m)
}

func sequence(writer *bufio.Writer, result []int, index, n, m int) {
	if index == m {
		for i := 0; i < m; i++ {
			fmt.Fprintf(writer, "%d ", result[i])
		}
		fmt.Fprint(writer, "\n")
		return
	}
	for i := 1; i < n+1; i++ {
		if index > 0 && result[index-1] > i {
			continue
		}
		result[index] = i
		sequence(writer, result, index+1, n, m)
	}
}
728x90

'dev > algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ / 2580๋ฒˆ / ์Šค๋„์ฟ  [Go]  (0) 2020.07.24
BOJ / 9663๋ฒˆ / N-Queen [Go]  (0) 2020.07.23
BOJ / 15651๋ฒˆ / N๊ณผ M (3) [Go]  (0) 2020.07.21
BOJ / 15650๋ฒˆ / N๊ณผ M (2) [Go]  (0) 2020.07.20
BOJ / 15649๋ฒˆ / N๊ณผ M (1) [Go]  (0) 2020.07.19
๋Œ“๊ธ€