๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์žฌ๊ท€ํ˜ธ์ถœ2

์ค‘์ฒฉ ๋ฐ˜๋ณต๋ฌธ ๋Œ€์ฒดํ•˜๊ธฐ Q. 0๋ฒˆ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋ฒˆํ˜ธ ๋งค๊ฒจ์ง„ n๊ฐœ์˜ ์›์†Œ ์ค‘ ๋„ค ๊ฐœ๋ฅผ ๊ณ ๋ฅด๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๊ตฌํ•ด๋ผ. ์˜ˆ๋ฅผ ๋“ค์–ด n์ด 5์ธ ๊ฒฝ์šฐ, 0~4๊นŒ์ง€์˜ ์›์†Œ ์ค‘ ์ฐจ๋ก€๋กœ ๋„ค ๊ฐœ๋ฅผ ๊ณ ๋ฅด๋ฏ€๋กœ (0 1 2 3), (0 1 2 4), (0 1 3 4), (0 2 3 4), (1 2 3 4)์˜ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. A1. ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์ฐจ๋ก€๋กœ ๋„ค ๊ฐœ๋ฅผ ๊ณ ๋ฅด๋ฏ€๋กœ 4์ค‘ ์ค‘์ฒฉ for๋ฌธ์„ ์จ์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. class Test { public static void main(String[] args) { int n = 5; // n์ด 5์ธ ๊ฒฝ์šฐ 0~4๊นŒ์ง€ ์›์†Œ ์ค‘ ์ฐจ๋ก€๋กœ 4๊ฐœ๋ฅผ ๊ณจ๋ผ ์ถœ๋ ฅํ•จ // ์ฒซ ๋ฒˆ์งธ ์›์†Œ ์„ ํƒ for (int i = 0; i < n; i++) { // ๋‘ ๋ฒˆ์งธ ์›์†Œ ์„ ํƒ (์ฐจ๋ก€๋Œ€๋กœ ๊ณ ๋ฅด๋ฏ€๋กœ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ณด๋‹ค ์ปค์•ผ ํ•จ).. 2021. 10. 14.
1๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ Q. ์ž์—ฐ์ˆ˜ n์ด ์ฃผ์–ด์กŒ์„ ๋•Œ 1๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ sum()์„ ๊ตฌํ˜„ํ•˜์‹œ์˜ค. A1. ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• for๋ฌธ ๋‚ด์—์„œ i๊ฐ€ 1๋ถ€ํ„ฐ 5๊นŒ์ง€ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉฐ result ๋ณ€์ˆ˜์— ๋ˆ„์ ํ•ฉ์„ ์ €์žฅํ•œ๋‹ค. class Test { public static void main(String[] args) { System.out.println("1~5 ๊นŒ์ง€์˜ ํ•ฉ๊ณ„: " + sum(5)); } public static int sum(int number) { int result = 0; for (int i = 1; i 2021. 10. 13.