Skip to content

Commit

Permalink
[level 1] Title: 약수의 개수와 덧셈, Time: 0.16 ms, Memory: 78.3 MB -BaekjoonHub
Browse files Browse the repository at this point in the history
  • Loading branch information
shinjaewon99 committed Sep 3, 2023
1 parent f1e5654 commit 91b1508
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# [level 1] 약수의 개수와 덧셈 - 77884

[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/77884)

### 성능 요약

메모리: 78.3 MB, 시간: 0.16 ms

### 구분

코딩테스트 연습 > 월간 코드 챌린지 시즌2

### 채점결과

Empty

### 문제 설명

<p>두 정수 <code>left</code>와 <code>right</code>가 매개변수로 주어집니다. <code>left</code>부터 <code>right</code>까지의 모든 수들 중에서, 약수의 개수가 짝수인 수는 더하고, 약수의 개수가 홀수인 수는 뺀 수를 return 하도록 solution 함수를 완성해주세요.</p>

<hr>

<h5>제한사항</h5>

<ul>
<li>1 ≤ <code>left</code> ≤ <code>right</code> ≤ 1,000</li>
</ul>

<hr>

<h5>입출력 예</h5>
<table class="table">
<thead><tr>
<th>left</th>
<th>right</th>
<th>result</th>
</tr>
</thead>
<tbody><tr>
<td>13</td>
<td>17</td>
<td>43</td>
</tr>
<tr>
<td>24</td>
<td>27</td>
<td>52</td>
</tr>
</tbody>
</table>
<hr>

<h5>입출력 예 설명</h5>

<p><strong>입출력 예 #1</strong></p>

<ul>
<li>다음 표는 13부터 17까지의 수들의 약수를 모두 나타낸 것입니다.</li>
</ul>
<table class="table">
<thead><tr>
<th>수</th>
<th>약수</th>
<th>약수의 개수</th>
</tr>
</thead>
<tbody><tr>
<td>13</td>
<td>1, 13</td>
<td>2</td>
</tr>
<tr>
<td>14</td>
<td>1, 2, 7, 14</td>
<td>4</td>
</tr>
<tr>
<td>15</td>
<td>1, 3, 5, 15</td>
<td>4</td>
</tr>
<tr>
<td>16</td>
<td>1, 2, 4, 8, 16</td>
<td>5</td>
</tr>
<tr>
<td>17</td>
<td>1, 17</td>
<td>2</td>
</tr>
</tbody>
</table>
<ul>
<li>따라서, 13 + 14 + 15 - 16 + 17 = 43을 return 해야 합니다.</li>
</ul>

<p><strong>입출력 예 #2</strong></p>

<ul>
<li>다음 표는 24부터 27까지의 수들의 약수를 모두 나타낸 것입니다.</li>
</ul>
<table class="table">
<thead><tr>
<th>수</th>
<th>약수</th>
<th>약수의 개수</th>
</tr>
</thead>
<tbody><tr>
<td>24</td>
<td>1, 2, 3, 4, 6, 8, 12, 24</td>
<td>8</td>
</tr>
<tr>
<td>25</td>
<td>1, 5, 25</td>
<td>3</td>
</tr>
<tr>
<td>26</td>
<td>1, 2, 13, 26</td>
<td>4</td>
</tr>
<tr>
<td>27</td>
<td>1, 3, 9, 27</td>
<td>4</td>
</tr>
</tbody>
</table>
<ul>
<li>따라서, 24 - 25 + 26 + 27 = 52를 return 해야 합니다.</li>
</ul>


> 출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public int solution(int left, int right) {
int answer = 0;

// 1. left와 right 매개변수 만큼 반복
for(int i = left; i <= right; i++){
int count = 0;
// 2. 약수의 갯수 판별
for(int j = 1; j <= i; j++){
if(i % j == 0){
count++;
}
}
// 3. 약수의 갯수가 짝수일 경우
if(count % 2 == 0){
answer += i;
}
// 4. 약수의 갯수가 홀수일 경우
else{
answer -= i;
}
}
return answer;
}
}

0 comments on commit 91b1508

Please sign in to comment.