๐Ÿค”<ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ ˆ๋ฒจ 1>ํ‰๊ท  ๊ตฌํ•˜๊ธฐ

์ง„์„ญยท2021๋…„ 12์›” 31์ผ
0
post-thumbnail
post-custom-banner

๐Ÿ“Œ ๋ฌธ์ œ์„ค๋ช…

์ •์ˆ˜๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด arr์˜ ํ‰๊ท ๊ฐ’์„ returnํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด๋ณด์„ธ์š”.

๐Ÿ“Œ์ œํ•œ์‚ฌํ•ญ

  • arr์€ ๊ธธ์ด 1 ์ด์ƒ, 100 ์ดํ•˜์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • arr์˜ ์›์†Œ๋Š” -10,000 ์ด์ƒ 10,000 ์ดํ•˜์ธ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.

๐Ÿ“Œ์ž…์ถœ๋ ฅ ์˜ˆ

๐Ÿ“Œ๋‚ด ์ฝ”๋“œ

class Solution {
    fun solution(arr: IntArray): Double {
        var answer = 0

        for(i in arr){
            answer = answer + i // ๋ฐฐ์—ด์— ์žˆ๋Š” ์š”์†Œ ๋‹ค ๋”ํ•ด์„œ answer์— ๋„ฃ์–ด์ค€๋‹ค.
        }
        return answer /arr.size.toDouble() //arr ์‚ฌ์ด์ฆˆ๋ฅผ ๊ตฌํ•œ ํ›„ double๋กœ ๋ณ€ํ™˜ ํ›„ ์ด ๊ฐ’์„ ๋‚˜๋ˆ ์ค€ ํ›„ ๋ฆฌํ„ดํ•ด ์ค€๋‹ค.
    }
}

๐Ÿ“Œ๋‹ค๋ฅธ ์‚ฌ๋žŒ ์ฝ”๋“œ 1

class Solution {
    fun solution(arr: IntArray): Double 
    {
        return arr.average()//.average()๋กœ ํ‰๊ท  ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    }
}
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€