๋ฌธ์
ํ์ด
๊ทธ๋ฆผ์ผ๋ก ๋ดค์ ๋ ์ดํด๊ฐ ๊ฐ๋๋ฐ
์ฝ๋๋ก ๋ณด๋ ์ ์ด๋ฐ์ง ํ๋ํ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ ๊ฒ ์ข๊ฒ ๋ค๊ณ ์๊ฐํ๋ค.
์ด ๋ฌธ์ ๋ ๋ฐฑ์ค์ 2018๋ฒ ๋ฌธ์ โ์๋ค์ ํฉ5โ์ด๋ค.
ํฌ ํฌ์ธํฐ ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํด ํ์ด๋ณผ ๊ฒ์ด๋ค.
start ํฌ์ธํฐ์, end ํฌ์ธํธ๋ฅผ 1์ ๋๊ณ ์์ํ๋ค.
์ฐ์๋ ์ซ์์ ํฉ์ ๊ตฌํ๋ ๋ฌธ์ ์ด๋
start ~ end ๊น์ง์ ํฉ์ ๊ตฌํ๋ ๋ฐฉ๋ฒ์ ์ด์ฉํ๋ค.
์ผ๋จ ์๋ ์ฝ๋๋ก๋ ์ด๋ ๊ฒ ๋๋ค๊ณ ํ๋ค. - ๋์์ฑ ์ฐธ๊ณ
1
2
3
4
5
6
7
8
N ๋ณ์ ์ ์ฅ
์ฌ์ฉ ๋ณ์ ์ด๊ธฐํ(count = 1, start_index = 1, end_index = 1, sum = 1)
while(end_index != N) {
if(sum == N) count ์ฆ๊ฐ, end_index ์ฆ๊ฐ, sum๊ฐ ๋ณ๊ฒฝ
else if(sum > N) sum๊ฐ ๋ณ๊ฒฝ, start_index ์ฆ๊ฐ
else if(sum < N) end_index ์ฆ๊ฐ, sum๊ฐ ๋ณ๊ฒฝ
}
count ์ถ๋ ฅํ๊ธฐ
๊ตฌํํ ์ฝ๋๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Q2018_์๋ค์ํฉ5 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int start = 1, end = 1, sum = 1, count = 1;
while (start != N) {
if (sum == N) {
count++;
end++;
sum += end;
} else if (sum > N) {
sum -= start;
start++;
} else if (sum < N){
end++;
sum += end;
}
}
System.out.println(count);
}
}
/*
----- ์์ ์
๋ ฅ 1 -----
15
----- ์์ ์ถ๋ ฅ 1 -----
4
*/
์์ ์ธ 15๋ ๋๋ฌด ํฌ๋
N = 5 ๋ผ ๊ฐ์ ํด๋ณด๊ฒ ๋ค.
๋์ฌ ์ ์๋ ๊ฐ์ (2,3), (5)๋ก
count = 2์ด ์ ๋ต์ด๋ค.
- ์ฒ์์ผ๋ก๋ N ๊ฐ ์์ฒด์ธ (5)์ธ ๊ฒฝ์ฐ๋ฅผ count์ ๋ฏธ๋ฆฌ ๋ฃ์ด์ฃผ๊ณ ์์ํ๋ค.
- start ํฌ์ธํฐ๊ฐ 1~ ๋ฐ๋ณตํ๊ฒ ๋๊ณ
- ๋์ด์ start ํฌ์ธํฐ 1๋ก ํ ์ ์๋ ๊ฒ ์๋ค๋ฉด,
1์ ๋ํด 2๋ก start ํฌ์ธํฐ๋ฅผ ์ฎ๊ฒจ์ ์งํํ๋ค. - end ํฌ์ธํฐ๋ฅผ ๋๋ฌด ํฌ๊ฒ ์ก์
์๋ค์ ํฉ์ธ sum์ด ๊ตฌํ๋ ค๋ ์๋ณด๋ค ์ปค์ง๋ค๋ฉด,
ํด๋น end ํฌ์ธํฐ๋ฅผ ์์ผ๋ก ํ์นธ ์ฎ๊ฒจ ์งํํ๋ค. - ์ํ๋ ๊ฐ์ ์ฐพ์ count๋ฅผ ์ฌ๋ ค์ฃผ์๋ค๋ฉด
๋ค์ ๊ฒฝ์ฐ๋ฅผ ์ฐพ์์ผ ํ๋ฏ๋ก
end ํฌ์ธํฐ๋ฅผ ๋ค๋ก ํ์นธ ์ฎ๊ฒจ ๋ค์ ๊ฒฝ์ฐ๋ฅผ ์ฐพ๊ธฐ ์์ํ๋ค.
(5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Q2018_์๋ค์ํฉ5 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int start = 1, end = 1, sum = 1, count = 1; // ๐ ๋ณธ์ธ์ธ ์ซ์ N์ ๋ฏธ๋ฆฌ ์นด์ดํธํด ๋๊ณ
while (start != N) { // ์ฌ๊ธฐ์์๋ ๋ณธ์ธ ์ซ์N์ ์ธ๋ ์ผ์ ํ์ง ์๋๋ค.
if (sum == N) {
count++;
end++;
sum += end;
} else if (sum > N) {
sum -= start;
start++;
} else if (sum < N){
end++;
sum += end;
}
}
System.out.println(count);
}
}
์ด์ ์ด๊ธฐ ์ซ์์ธ 1์ ๋ฃ์ด while๋ฌธ์ ์คํํด๋ณธ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Q2018_์๋ค์ํฉ5 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int start = 1, end = 1, sum = 1, count = 1;
// N = 5, start = 1
while (start != N) {
// case A (sum = N)
if (sum == N) {
count++;
end++;
sum += end;
// 4๏ธโฃ sum(5) = N(5)
// ๐ count(2)
// end(4)
// sum(9) = sum(5) + end(4)
//---> start(2), end(4), sum(9), count(2)
// case B (sum > N)
} else if (sum > N) {
sum -= start;
start++;
// 3๏ธโฃ sum(6) > N(5)
// sum(5) = sum(6) - start(1)
// start(2)
//---> start(2), end(3), sum(5), count(1)
// ๐ (2, 3)
// case C (sum < N)
} else if (sum < N){
end++;
sum += end;
// 1๏ธโฃ sum(1) < N(5)
// end(2)
// sum(3) = sum(1) + end(2)
//---> start(1), end(2), sum(3), count(1)
// 2๏ธโฃ sum(3) < N(5)
// end(3)
// sum(6) = sum(3) + end(3)
//---> start(1), end(3), sum(6), count(1)
}
}
System.out.println(count);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Q2018_์๋ค์ํฉ5 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int start = 1, end = 1, sum = 1, count = 1;
// N = 5, start(2), end(4), sum(9), count(2)
while (start != N) { // 5๏ธโฃ start(5) = N(5) ---> ์ข
๋ฃ
// case A (sum = N)
if (sum == N) {
count++;
end++;
sum += end;
// case B (sum > N)
} else if (sum > N) {
sum -= start;
start++;
// 1๏ธโฃ sum(9) > N(5)
// sum(7) = sum(9) - start(2)
// start(3)
//---> start(3), end(4), sum(7), count(2)
// 2๏ธโฃ sum(7) > N(5)
// sum(4) = sum(7) - start(3)
// start(4)
//---> start(4), end(4), sum(4), count(2)
// 4๏ธโฃ sum(9) > N(5)
// sum(5) = sum(9) - start(4)
// start(5)
//---> start(5), end(5), sum(5), count(2)
// case C (sum < N)
} else if (sum < N){
end++;
sum += end;
// 3๏ธโฃ sum(4) < N(5)
// end(5)
// sum(9) = sum(4) + end(5)
//---> start(4), end(5), sum(9), count(2)
}
}
System.out.println(count);
}
}
๋ต์ 2!
๊ฒฐ๊ณผ
- ์์ ์ธ N์ ๋ฏธ๋ฆฌ count ํด๋๊ณ
- ๊ทธ์ธ์ ๊ฒฝ์ฐ๋ฅผ ๊ตฌํด countํด์ฃผ๊ณ
- ๋ง์ง๋ง์ start(5) = end(5) = sum(5) ๊ฐ ๋๋ฉด์ while๋ฌธ ์ข ๋ฃ