Home ๐Ÿฃ ๋ฐฑ์ค€ Q2018 - ์ˆ˜๋“ค์˜ ํ•ฉ5 (java)
Post
Cancel

๐Ÿฃ ๋ฐฑ์ค€ Q2018 - ์ˆ˜๋“ค์˜ ํ•ฉ5 (java)

๋ฌธ์ œ




แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-03-01 แ„‹แ…ฉแ„’แ…ฎ 10 12 26

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-03-01 แ„‹แ…ฉแ„’แ…ฎ 10 12 04



ํ’€์ด


๊ทธ๋ฆผ์œผ๋กœ ๋ดค์„ ๋• ์ดํ•ด๊ฐ€ ๊ฐ”๋Š”๋ฐ
์ฝ”๋“œ๋กœ ๋ณด๋‹ˆ ์™œ ์ด๋Ÿฐ์ง€ ํ•˜๋‚˜ํ•˜๋‚˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋Š” ๊ฒŒ ์ข‹๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

์ด ๋ฌธ์ œ๋Š” ๋ฐฑ์ค€์˜ 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์ด ์ •๋‹ต์ด๋‹ค.

  1. ์ฒ˜์Œ์œผ๋กœ๋Š” N ๊ฐ’ ์ž์ฒด์ธ (5)์ธ ๊ฒฝ์šฐ๋ฅผ count์— ๋ฏธ๋ฆฌ ๋„ฃ์–ด์ฃผ๊ณ  ์‹œ์ž‘ํ•œ๋‹ค.
  2. start ํฌ์ธํ„ฐ๊ฐ€ 1~ ๋ฐ˜๋ณตํ•˜๊ฒŒ ๋˜๊ณ 
  3. ๋”์ด์ƒ start ํฌ์ธํ„ฐ 1๋กœ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒŒ ์—†๋‹ค๋ฉด,
    1์„ ๋”ํ•ด 2๋กœ start ํฌ์ธํ„ฐ๋ฅผ ์˜ฎ๊ฒจ์„œ ์ง„ํ–‰ํ•œ๋‹ค.
  4. end ํฌ์ธํ„ฐ๋ฅผ ๋„ˆ๋ฌด ํฌ๊ฒŒ ์žก์•„
    ์ˆ˜๋“ค์˜ ํ•ฉ์ธ sum์ด ๊ตฌํ•˜๋ ค๋Š” ์ˆ˜๋ณด๋‹ค ์ปค์ง„๋‹ค๋ฉด,
    ํ•ด๋‹น end ํฌ์ธํ„ฐ๋ฅผ ์•ž์œผ๋กœ ํ•œ์นธ ์˜ฎ๊ฒจ ์ง„ํ–‰ํ•œ๋‹ค.
  5. ์›ํ•˜๋Š” ๊ฐ’์„ ์ฐพ์•„ 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!

๊ฒฐ๊ณผ


แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-02-27 แ„‹แ…ฉแ„’แ…ฎ 4 05 58

  1. ์ž์‹ ์ธ N์€ ๋ฏธ๋ฆฌ count ํ•ด๋‘๊ณ 
  2. ๊ทธ์™ธ์˜ ๊ฒฝ์šฐ๋ฅผ ๊ตฌํ•ด countํ•ด์ฃผ๊ณ 
  3. ๋งˆ์ง€๋ง‰์€ start(5) = end(5) = sum(5) ๊ฐ€ ๋˜๋ฉด์„œ while๋ฌธ ์ข…๋ฃŒ
This post is licensed under CC BY 4.0 by the author.

๐Ÿฃ ๋ฐฑ์ค€ Q11659 - ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 4 (java)

๐Ÿฃ ๋ฐฑ์ค€ Q11660 - ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 5 (java)