dev/algorithm
BOJ / 1392ė² / ė øė ģ 볓 [Go][Python3]
crscnt
2020. 12. 19. 21:00
š©š»š» 문ģ
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