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

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

 

1392๋ฒˆ: ๋…ธ๋ž˜ ์•…๋ณด

์ฒซ ์ค„์—๋Š” ์•…๋ณด ์ˆ˜ N(1 ≤ N ≤ 100)๊ณผ ์งˆ๋ฌธ์˜ ๊ฐœ์ˆ˜ Q(1 ≤ Q ≤ 1,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” 1๋ฒˆ ์•…๋ณด๋ถ€ํ„ฐ N๋ฒˆ ์•…๋ณด๊นŒ์ง€ ๊ฐ ์•…๋ณด๊ฐ€ ์ฐจ์ง€ํ•˜๋Š” ์‹œ๊ฐ„(์ดˆ)์ด ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ์•…๋ณด๊ฐ€

www.acmicpc.net


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

๐ŸŽจ Go

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

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

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

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

	var scores []int
	for i := 0; i < n; i++ {
		var seconds int
		fmt.Fscanln(reader, &seconds)
		if i == 0 {
			scores = append(scores, seconds)
		} else {
			scores = append(scores, scores[i-1]+seconds)
		}
	}

	var questions []int
	for i := 0; i < q; i++ {
		var seconds int
		fmt.Fscanln(reader, &seconds)
		questions = append(questions, seconds)
	}

	for i := 0; i < q; i++ {
		var question = questions[i]
		for j := 0; j < n; j++ {
			if scores[j] > question {
				fmt.Fprintln(writer, j+1)
				break
			}
		}
	}
}

๐ŸŽจ Python3

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

if __name__ == "__main__":
    n, q = list(map(int, sys.stdin.readline().split()))
    scores = []
    for i in range(n):
        seconds = int(sys.stdin.readline())
        if i == 0:
            scores.append(seconds)
        else:
            scores.append(scores[i-1]+seconds)

    questions = []
    for i in range(q):
        seconds = int(sys.stdin.readline())
        questions.append(seconds)
        
    for i in range(q):
        question = questions[i]
        for j in range(n):
            if scores[j] > question:
                print(j+1)
                break
728x90
๋Œ“๊ธ€