Home ๊ฐœ๋ฏธ ๊ตฐ๋‹จ
Post
์ทจ์†Œ

๊ฐœ๋ฏธ ๊ตฐ๋‹จ

๐Ÿ”– ๊ฐœ๋ฏธ ๊ตฐ๋‹จ

๐Ÿ“Œ ๋ฌธ์ œ

๋ฌธ์ œ ์„ค๋ช…

๊ฐœ๋ฏธ ๊ตฐ๋‹จ์ด ์‚ฌ๋ƒฅ์„ ๋‚˜๊ฐ€๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ฐœ๋ฏธ๊ตฐ๋‹จ์€ ์‚ฌ๋ƒฅ๊ฐ์˜ ์ฒด๋ ฅ์— ๋”ฑ ๋งž๋Š” ๋ณ‘๋ ฅ์„ ๋ฐ๋ฆฌ๊ณ  ๋‚˜๊ฐ€๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์žฅ๊ตฐ๊ฐœ๋ฏธ๋Š” 5์˜ ๊ณต๊ฒฉ๋ ฅ์„, ๋ณ‘์ •๊ฐœ๋ฏธ๋Š” 3์˜ ๊ณต๊ฒฉ๋ ฅ์„ ์ผ๊ฐœ๋ฏธ๋Š” 1์˜ ๊ณต๊ฒฉ๋ ฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ฒด๋ ฅ 23์˜ ์—ฌ์น˜๋ฅผ ์‚ฌ๋ƒฅํ•˜๋ ค๊ณ  ํ•  ๋•Œ, ์ผ๊ฐœ๋ฏธ 23๋งˆ๋ฆฌ๋ฅผ ๋ฐ๋ฆฌ๊ณ  ๊ฐ€๋„ ๋˜์ง€๋งŒ, ์žฅ๊ตฐ๊ฐœ๋ฏธ ๋„ค ๋งˆ๋ฆฌ์™€ ๋ณ‘์ •๊ฐœ๋ฏธ ํ•œ ๋งˆ๋ฆฌ๋ฅผ ๋ฐ๋ฆฌ๊ณ  ๊ฐ„๋‹ค๋ฉด ๋” ์ ์€ ๋ณ‘๋ ฅ์œผ๋กœ ์‚ฌ๋ƒฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ๋ƒฅ๊ฐ์˜ ์ฒด๋ ฅ hp๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์‚ฌ๋ƒฅ๊ฐ์˜ ์ฒด๋ ฅ์— ๋”ฑ ๋งž๊ฒŒ ์ตœ์†Œํ•œ์˜ ๋ณ‘๋ ฅ์„ ๊ตฌ์„ฑํ•˜๋ ค๋ฉด ๋ช‡ ๋งˆ๋ฆฌ์˜ ๊ฐœ๋ฏธ๊ฐ€ ํ•„์š”ํ•œ์ง€๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.


์ œํ•œ์‚ฌํ•ญ
  • hp๋Š” ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • 0 โ‰ค hp โ‰ค 1000

์ž…์ถœ๋ ฅ ์˜ˆ
hpresult
235
246
999201

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • hp๊ฐ€ 23์ด๋ฏ€๋กœ, ์žฅ๊ตฐ๊ฐœ๋ฏธ ๋„ค๋งˆ๋ฆฌ์™€ ๋ณ‘์ •๊ฐœ๋ฏธ ํ•œ๋งˆ๋ฆฌ๋กœ ์‚ฌ๋ƒฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 5๋ฅผ returnํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • hp๊ฐ€ 24์ด๋ฏ€๋กœ, ์žฅ๊ตฐ๊ฐœ๋ฏธ ๋„ค๋งˆ๋ฆฌ ๋ณ‘์ •๊ฐœ๋ฏธ ํ•œ๋งˆ๋ฆฌ ์ผ๊ฐœ๋ฏธ ํ•œ๋งˆ๋ฆฌ๋กœ ์‚ฌ๋ƒฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 6์„ returnํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #3

  • hp๊ฐ€ 999์ด๋ฏ€๋กœ, ์žฅ๊ตฐ๊ฐœ๋ฏธ 199 ๋งˆ๋ฆฌ ๋ณ‘์ •๊ฐœ๋ฏธ ํ•œ๋งˆ๋ฆฌ ์ผ๊ฐœ๋ฏธ ํ•œ๋งˆ๋ฆฌ๋กœ ์‚ฌ๋ƒฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 201์„ returnํ•ฉ๋‹ˆ๋‹ค.

โœ๏ธ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function solution(hp) {
    var answer = 0;
    const antArmy = { 
      generalAnt: "5",   
      soldierAnt: "3",
      ergate: "1"
    };
    
    for(const v in antArmy) {
        answer += parseInt(hp / antArmy[v])
        hp %= antArmy[v]
    }
  
    return answer;
}

์žฅ๊ตฐ ๊ฐœ๋ฏธ์˜ ๊ณต๊ฒฉ๋ ฅ 5, ๋ณ‘์žฅ ๊ฐœ๋ฏธ์˜ ๊ณต๊ฒฉ๋ ฅ์ด 3, ์ผ๊ฐœ๋ฏธ์˜ ๊ณต๊ฒฉ๋ ฅ์ด 1๋กœ ์ฃผ์–ด์กŒ์œผ๋ฏ€๋กœ, ์ด ๊ณต๊ฒฉ๋ ฅ์€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ๋‚˜๋Š” const๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ๊ทธ ๋‹ค์Œ ๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋Š” forโ€ฆin ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•˜์—ฌ ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•˜๊ณ  ์ด๊ฒƒ์„ ๋ฐ˜๋ณตํ•˜์˜€๋‹ค.

๐Ÿ” ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

1
2
3
4
5
6
7
// ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด
function solution(hp) {
    const ์žฅ๊ตฐ๊ฐœ๋ฏธ = Math.floor(hp / 5);
    const ๋ณ‘์ •๊ฐœ๋ฏธ = Math.floor((hp - (์žฅ๊ตฐ๊ฐœ๋ฏธ * 5)) / 3);
    const ์ผ๊ฐœ๋ฏธ = hp - ((์žฅ๊ตฐ๊ฐœ๋ฏธ * 5) + (๋ณ‘์ •๊ฐœ๋ฏธ * 3));
    return ์žฅ๊ตฐ๊ฐœ๋ฏธ+๋ณ‘์ •๊ฐœ๋ฏธ+์ผ๊ฐœ๋ฏธ;
}

์ด ํ’€์ด์—์„œ๋Š” ์žฅ๊ตฐ๊ฐœ๋ฏธ์™€ ๋ณ‘์ •๊ฐœ๋ฏธ, ์ผ๊ฐœ๋ฏธ๋ฅผ ๊ฐ๊ฐ ๊ตฌํ•˜๊ณ  ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•˜์—ฌ return๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ์—ˆ๋‹ค.

๐Ÿ’ป ์ถœ๋ ฅ ๊ฒฐ๊ณผ

image-20221126163937530

This post is licensed under CC BY 4.0 by the author.

๊ฐ€์œ„ ๋ฐ”์œ„ ๋ณด

๋ชจ์Šค๋ถ€ํ˜ธ (1)