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

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

 

2503๋ฒˆ: ์ˆซ์ž ์•ผ๊ตฌ

์ฒซ์งธ ์ค„์—๋Š” ๋ฏผํ˜์ด๊ฐ€ ์˜์ˆ˜์—๊ฒŒ ๋ช‡ ๋ฒˆ์ด๋‚˜ ์งˆ๋ฌธ์„ ํ–ˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” 1 ์ด์ƒ 100 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. ์ด์–ด์ง€๋Š” N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ์ค„๋งˆ๋‹ค ๋ฏผํ˜์ด๊ฐ€ ์งˆ๋ฌธํ•œ ์„ธ ์ž๋ฆฌ ์ˆ˜์™€ ์˜์ˆ˜๊ฐ€ ๋‹ตํ•œ ์ŠคํŠธ

www.acmicpc.net


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

๐ŸŽจ Go

// https://www.acmicpc.net/problem/2503
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanln(reader, &n)

	var games []Game
	for i := 0; i < n; i++ {
		game := Game{}
		fmt.Fscanln(reader, &game.number, &game.strike, &game.ball)
		games = append(games, game)
	}

	var count int                 // ๊ฐ€๋Šฅํ•œ ๋‹ต์˜ ๊ฐœ์ˆ˜
	for i := 123; i <= 987; i++ { // ๋ชจ๋“  ๊ฒฝ์šฐ ๊ณ„์‚ฐ
		var isAnswer bool = true
		a, b, c := i/100, (i/10)%10, i%10
		if !checkValid(a, b, c) { // ๊ฐ ์ž๋ฆฟ์ˆ˜๊ฐ€ 1-9์˜ ์„œ๋กœ ๋‹ค๋ฅธ ์ˆซ์ž ์„ธ๊ฐœ๋กœ ๊ตฌ์„ฑ๋˜์—ˆ๋Š”์ง€ ์ฒดํฌ
			continue
		}

		for j := 0; j < n; j++ { // i๊ฐ’์ด ๋ฏผํ˜์ด์˜ ์งˆ๋ฌธ๊ณผ ์˜์ˆ˜์˜ ๋Œ€๋‹ต์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์— ๋Œ€ํ•ด ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ count ์ฆ๊ฐ€
			strike, ball, number := 0, 0, games[j].number
			a2, b2, c2 := number/100, number/10%10, number%10
			if a == a2 {
				strike++
			}
			if a == b2 || a == c2 {
				ball++
			}
			if b == b2 {
				strike++
			}
			if b == a2 || b == c2 {
				ball++
			}
			if c == c2 {
				strike++
			}
			if c == a2 || c == b2 {
				ball++
			}
			if !(strike == games[j].strike && ball == games[j].ball) {
				isAnswer = false
				break
			}
		}
		if isAnswer {
			count++
		}
	}

	fmt.Fprintln(writer, count)
}

type Game struct {
	number int
	strike int
	ball   int
}

func checkValid(a, b, c int) bool {
	if b == 0 || c == 0 {
		return false
	}
	if a == b || b == c || c == a {
		return false
	}
	return true
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/2503
import sys

def check_valid(a, b, c):
    if b == 0 or c == 0:
        return False
    if a == b or b == c or c == a:
        return False
    return True

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    games = []
    for i in range(n):
        games.append(list(map(int, sys.stdin.readline().split())))
    
    count = 0
    for i in range(123, 988):
        is_answer = True
        a, b, c = i//100, (i//10)%10, i%10
        if not check_valid(a, b, c):
            continue

        for j in range(0, n):
            strike, ball, number = 0, 0, games[j][0]
            a2, b2, c2 = number//100, number//10%10, number%10
            if a == a2:
                strike += 1
            if a == b2 or a == c2:
                ball += 1
            if b == b2:
                strike += 1
            if b == a2 or b == c2:
                ball += 1
            if c == c2:
                strike += 1
            if c == a2 or c == b2:
                ball += 1
            if not (strike == games[j][1] and ball == games[j][2]):
                is_answer = False
                break
        if is_answer:
            count += 1
    print(count)
728x90
๋Œ“๊ธ€