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

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

 

1789๋ฒˆ: ์ˆ˜๋“ค์˜ ํ•ฉ

์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ S(1 ≤ S ≤ 4,294,967,295)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net


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

๐ŸŽจ Go

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

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

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

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

	var start, end int = 1, s
	var mid int
	var answer int
	for start <= end {
		mid = (start + end) / 2
		if mid*(mid+1)/2 <= s {
			answer = mid
			start = mid + 1
		} else {
			end = mid - 1
		}
	}
	fmt.Fprintln(writer, answer)
}

๐ŸŽจ Python3

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

if __name__ == "__main__":
    s = int(sys.stdin.readline())
    start, end = 1, s
    mid, answer = 0, 0
    while start <= end:
        mid = (start + end) // 2
        if mid*(mid + 1) // 2 <= s:
            answer = mid
            start = mid + 1
        else:
            end = mid - 1
    print(answer)
728x90
๋Œ“๊ธ€