Skip to content

Commit c46d024

Browse files
committed
[level 0] Title: 홀짝에 따라 다른 값 반환하기, Time: 0.06 ms, Memory: 87.7 MB -BaekjoonHub
1 parent 9de3dd5 commit c46d024

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# [level 0] 홀짝에 따라 다른 값 반환하기 - 181935
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/181935)
4+
5+
### 성능 요약
6+
7+
메모리: 87.7 MB, 시간: 0.06 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 코딩 기초 트레이닝
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2026년 03월 19일 10:02:35
20+
21+
### 문제 설명
22+
23+
<p>양의 정수 <code>n</code>이 매개변수로 주어질 때, <code>n</code>이 홀수라면 <code>n</code> 이하의 홀수인 모든 양의 정수의 합을 return 하고 <code>n</code>이 짝수라면 <code>n</code> 이하의 짝수인 모든 양의 정수의 제곱의 합을 return 하는 solution 함수를 작성해 주세요.</p>
24+
25+
<hr>
26+
27+
<h5>제한사항</h5>
28+
29+
<ul>
30+
<li>1 ≤ <code>n</code> ≤ 100</li>
31+
</ul>
32+
33+
<hr>
34+
35+
<h5>입출력 예</h5>
36+
<table class="table">
37+
<thead><tr>
38+
<th>n</th>
39+
<th>result</th>
40+
</tr>
41+
</thead>
42+
<tbody><tr>
43+
<td>7</td>
44+
<td>16</td>
45+
</tr>
46+
<tr>
47+
<td>10</td>
48+
<td>220</td>
49+
</tr>
50+
</tbody>
51+
</table>
52+
<hr>
53+
54+
<h5>입출력 예 설명</h5>
55+
56+
<p>입출력 예 #1</p>
57+
58+
<ul>
59+
<li>예제 1번의 <code>n</code>은 7로 홀수입니다. 7 이하의 모든 양의 홀수는 1, 3, 5, 7이고 이들의 합인 1 + 3 + 5 + 7 = 16을 return 합니다.</li>
60+
</ul>
61+
62+
<p>입출력 예 #2</p>
63+
64+
<ul>
65+
<li>예제 2번의 <code>n</code>은 10으로 짝수입니다. 10 이하의 모든 양의 짝수는 2, 4, 6, 8, 10이고 이들의 제곱의 합인 2<sup>2</sup> + 4<sup>2</sup> + 6<sup>2</sup> + 8<sup>2</sup> + 10<sup>2</sup> = 4 + 16 + 36 + 64 + 100 = 220을 return 합니다.</li>
66+
</ul>
67+
68+
69+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int solution(int n) {
3+
int answer = 0;
4+
5+
if(n%2!=0){
6+
for(int i=1; i<=n; i+=2){
7+
answer += i;
8+
}
9+
}
10+
else{
11+
for(int i=2; i<=n; i+=2){
12+
answer += Math.pow(i,2);
13+
}
14+
15+
16+
}
17+
return answer;
18+
}
19+
}

0 commit comments

Comments
 (0)