Problem Solution/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/JS] ํ”ผ์ž ๋‚˜๋ˆ  ๋จน๊ธฐ (1)

yuri lee 2023. 9. 6. 22:18
๋ฐ˜์‘ํ˜•

Problem Description

๋จธ์“ฑ์ด๋„ค ํ”ผ์ž๊ฐ€๊ฒŒ๋Š” ํ”ผ์ž๋ฅผ ์ผ๊ณฑ ์กฐ๊ฐ์œผ๋กœ ์ž˜๋ผ ์ค๋‹ˆ๋‹ค. ํ”ผ์ž๋ฅผ ๋‚˜๋ˆ ๋จน์„ ์‚ฌ๋žŒ์˜ ์ˆ˜ n์ด ์ฃผ์–ด์งˆ ๋•Œ, ๋ชจ๋“  ์‚ฌ๋žŒ์ด ํ”ผ์ž๋ฅผ ํ•œ ์กฐ๊ฐ ์ด์ƒ ๋จน๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ํ”ผ์ž์˜ ์ˆ˜๋ฅผ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”.

 

Restrictions

  • 1 ≤ n ≤ 100

 

Input/Output Example

  • ์ž…์ถœ๋ ฅ ์˜ˆ #1 7๋ช…์ด ์ตœ์†Œ ํ•œ ์กฐ๊ฐ์”ฉ ๋จน๊ธฐ ์œ„ํ•ด์„œ ์ตœ์†Œ 1ํŒ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
  • ์ž…์ถœ๋ ฅ ์˜ˆ #2 1๋ช…์€ ์ตœ์†Œ ํ•œ ์กฐ๊ฐ์„ ๋จน๊ธฐ ์œ„ํ•ด 1ํŒ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
  • ์ž…์ถœ๋ ฅ ์˜ˆ #2 15๋ช…์ด ์ตœ์†Œ ํ•œ ์กฐ๊ฐ์”ฉ ๋จน๊ธฐ ์œ„ํ•ด์„œ ์ตœ์†Œ 3ํŒ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

 

My solution

function solution(n) {
    if ((n % 7) >= 0) {
        return Math.ceil(n/7)
    } 
}

 

Another solutions

function solution(n) {
    return Math.ceil(n / 7)
}

ceil ํ•จ์ˆ˜ ๊ธฐ์–ตํ•˜๊ธฐ

console.log(Math.ceil(0.95));
// Expected output: 1

console.log(Math.ceil(4));
// Expected output: 4

console.log(Math.ceil(7.004));
// Expected output: 8

console.log(Math.ceil(-7.004));
// Expected output: -7

https://school.programmers.co.kr/learn/courses/30/lessons/120814

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil

 

Math.ceil() - JavaScript | MDN

The Math.ceil() static method always rounds up and returns the smallest integer greater than or equal to a given number.

developer.mozilla.org

 

๋ฐ˜์‘ํ˜•