๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐ˜์‘ํ˜•

Algorithm242

[๋ฐฑ์ค€] 11727๋ฒˆ: 2xn ํƒ€์ผ๋ง 2 https://www.acmicpc.net/problem/11727 11727๋ฒˆ: 2×n ํƒ€์ผ๋ง 2 ์ฒซ์งธ ์ค„์— 2×n ํฌ๊ธฐ์˜ ์ง์‚ฌ๊ฐํ˜•์„ ์ฑ„์šฐ๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ 10,007๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. www.acmicpc.net ์ฝ”๋“œ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[] dp = new int[1001]; dp[1] = 1; dp[2] = 3; for(int i=3; i dp[n-1] ๋งˆ์ง€๋ง‰ ๋„ค๋ชจ๋ฅผ 1X2 ์ง์‚ฌ๊ฐํ˜• ๋‘๊ฐœ๋กœ ๋†“๋Š” ๊ฒฝ์šฐ -> dp[n-2] ๊ทธ๋ฆฌ๊ณ  ์ถ”๊ฐ€์ ์œผ๋กœ ๋งˆ.. 2020. 3. 2.
[Codeforces] 677A: Vanya and Fence https://codeforces.com/problemset/problem/677/A Problem - 677A - Codeforces codeforces.com ์ฝ”๋“œ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int h = scan.nextInt(); int[] arr = new int[n]; int sum = 0; for(int i=0; i 2020. 3. 1.
[๋ฐฑ์ค€] 4641๋ฒˆ: Doubles(์™„์ „ ํƒ์ƒ‰) https://www.acmicpc.net/problem/4641 4641๋ฒˆ: Doubles ๋ฌธ์ œ 2~15๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ์ž์—ฐ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ์„ ๋•Œ, ์ด๋“ค ์ค‘ ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์ž์‹ ์˜ ์ •ํ™•ํžˆ 2๋ฐฐ์ธ ์ˆ˜๊ฐ€ ์žˆ๋Š” ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜์—ฌ๋ผ. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฆฌ์ŠคํŠธ๊ฐ€ "1 4 3 2 9 7 18 22"๋ผ๋ฉด 2๊ฐ€ 1์˜ 2๋ฐฐ, 4๊ฐ€ 2์˜ 2๋ฐฐ, 18์ด 9์˜ 2๋ฐฐ์ด๋ฏ€๋กœ ๋‹ต์€ 3์ด๋‹ค. ์ž…๋ ฅ ์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ฃผ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ์ž…๋ ฅ์˜ ๋์—๋Š” -1์ด ํ•˜๋‚˜ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, 2~15๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ์ž์—ฐ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. www.acmicpc.net ์ฝ”๋“œ import java.io.BufferedReader; import java.io.IOException; import java.. 2020. 2. 29.
[๋ฐฑ์ค€] 2156๋ฒˆ: ํฌ๋„์ฃผ ์‹œ์‹(DP) https://www.acmicpc.net/problem/2156 2156๋ฒˆ: ํฌ๋„์ฃผ ์‹œ์‹ ํšจ์ฃผ๋Š” ํฌ๋„์ฃผ ์‹œ์‹ํšŒ์— ๊ฐ”๋‹ค. ๊ทธ ๊ณณ์— ๊ฐ”๋”๋‹ˆ, ํ…Œ์ด๋ธ” ์œ„์— ๋‹ค์–‘ํ•œ ํฌ๋„์ฃผ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ํฌ๋„์ฃผ ์ž”์ด ์ผ๋ ฌ๋กœ ๋†“์—ฌ ์žˆ์—ˆ๋‹ค. ํšจ์ฃผ๋Š” ํฌ๋„์ฃผ ์‹œ์‹์„ ํ•˜๋ ค๊ณ  ํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‘ ๊ฐ€์ง€ ๊ทœ์น™์ด ์žˆ๋‹ค. ํฌ๋„์ฃผ ์ž”์„ ์„ ํƒํ•˜๋ฉด ๊ทธ ์ž”์— ๋“ค์–ด์žˆ๋Š” ํฌ๋„์ฃผ๋Š” ๋ชจ๋‘ ๋งˆ์…”์•ผ ํ•˜๊ณ , ๋งˆ์‹  ํ›„์—๋Š” ์›๋ž˜ ์œ„์น˜์— ๋‹ค์‹œ ๋†“์•„์•ผ ํ•œ๋‹ค. ์—ฐ์†์œผ๋กœ ๋†“์—ฌ ์žˆ๋Š” 3์ž”์„ ๋ชจ๋‘ ๋งˆ์‹ค ์ˆ˜๋Š” ์—†๋‹ค. ํšจ์ฃผ๋Š” ๋  ์ˆ˜ ์žˆ๋Š” ๋Œ€๋กœ ๋งŽ์€ ์–‘์˜ ํฌ๋„์ฃผ๋ฅผ ๋ง›๋ณด๊ธฐ ์œ„ํ•ด์„œ ์–ด๋–ค ํฌ๋„์ฃผ ์ž”์„ ์„ ํƒํ•ด์•ผ ํ• ์ง€ ๊ณ  www.acmicpc.net ์ฝ”๋“œ import java.util.Scanner; public class Main { public static void main(S.. 2020. 2. 28.
[๋ฐฑ์ค€] 2437๋ฒˆ: ์ €์šธ(๊ทธ๋ฆฌ๋””) https://www.acmicpc.net/problem/2437 2437๋ฒˆ: ์ €์šธ ํ•˜๋‚˜์˜ ์–‘ํŒ” ์ €์šธ์„ ์ด์šฉํ•˜์—ฌ ๋ฌผ๊ฑด์˜ ๋ฌด๊ฒŒ๋ฅผ ์ธก์ •ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ด ์ €์šธ์˜ ์–‘ ํŒ”์˜ ๋์—๋Š” ๋ฌผ๊ฑด์ด๋‚˜ ์ถ”๋ฅผ ์˜ฌ๋ ค๋†“๋Š” ์ ‘์‹œ๊ฐ€ ๋‹ฌ๋ ค ์žˆ๊ณ , ์–‘ํŒ”์˜ ๊ธธ์ด๋Š” ๊ฐ™๋‹ค. ๋˜ํ•œ, ์ €์šธ์˜ ํ•œ์ชฝ์—๋Š” ์ €์šธ์ถ”๋“ค๋งŒ ๋†“์„ ์ˆ˜ ์žˆ๊ณ , ๋‹ค๋ฅธ ์ชฝ์—๋Š” ๋ฌด๊ฒŒ๋ฅผ ์ธก์ •ํ•˜๋ ค๋Š” ๋ฌผ๊ฑด๋งŒ ์˜ฌ๋ ค๋†“์„ ์ˆ˜ ์žˆ๋‹ค. ๋ฌด๊ฒŒ๊ฐ€ ์–‘์˜ ์ •์ˆ˜์ธ N๊ฐœ์˜ ์ €์šธ์ถ”๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ์ด ์ถ”๋“ค์„ ์‚ฌ์šฉํ•˜์—ฌ ์ธก์ •ํ•  ์ˆ˜ ์—†๋Š” ์–‘์˜ ์ •์ˆ˜ ๋ฌด๊ฒŒ ์ค‘ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌด๊ฒŒ๊ฐ€ ๊ฐ๊ฐ 3, 1, 6, 2, 7, 30 www.acmicpc.net ์ฝ”๋“œ import java.util.Arrays; import java.util.Scanner; public class Main { publ.. 2020. 2. 28.
[๋ฐฑ์ค€] 1236๋ฒˆ: ์„ฑ ์ง€ํ‚ค๊ธฐ(๊ตฌํ˜„) https://www.acmicpc.net/problem/1236 1236๋ฒˆ: ์„ฑ ์ง€ํ‚ค๊ธฐ ์ฒซ์งธ ์ค„์— ์„ฑ์˜ ์„ธ๋กœ ํฌ๊ธฐ N๊ณผ ๊ฐ€๋กœ ํฌ๊ธฐ M์ด ์ฃผ์–ด์ง„๋‹ค. N๊ณผ M์€ 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์„ฑ์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์„ฑ์˜ ์ƒํƒœ๋Š” .์€ ๋นˆ์นธ, X๋Š” ๊ฒฝ๋น„์›์ด ์žˆ๋Š” ์นธ์ด๋‹ค. www.acmicpc.net ์ฝ”๋“œ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt();// ์„ธ๋กœ int M = scan.nextInt();// ๊ฐ€๋กœ int row = 0;// ํ–‰ int col = 0;.. 2020. 2. 27.
[๋ฐฑ์ค€] 1449๋ฒˆ: ์ˆ˜๋ฆฌ๊ณต ํ•ญ์Šน(๊ทธ๋ฆฌ๋””, ์ •๋ ฌ) https://www.acmicpc.net/problem/1449 1449๋ฒˆ: ์ˆ˜๋ฆฌ๊ณต ํ•ญ์Šน ์ฒซ์งธ ์ค„์— ๋ฌผ์ด ์ƒˆ๋Š” ๊ณณ์˜ ๊ฐœ์ˆ˜ N๊ณผ ํ…Œ์ดํ”„์˜ ๊ธธ์ด L์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๋ฌผ์ด ์ƒˆ๋Š” ๊ณณ์˜ ์œ„์น˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. N๊ณผ L์€ 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๊ณ , ๋ฌผ์ด ์ƒˆ๋Š” ๊ณณ์˜ ์œ„์น˜๋Š” 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. www.acmicpc.net ํ‹€๋ฆฐ ์ฝ”๋“œ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(St.. 2020. 2. 27.
[๋ฐฑ์ค€] 2193๋ฒˆ: ์ด์นœ์ˆ˜(DP) https://www.acmicpc.net/problem/2193 2193๋ฒˆ: ์ด์นœ์ˆ˜ 0๊ณผ 1๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์ˆ˜๋ฅผ ์ด์ง„์ˆ˜๋ผ ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์ง„์ˆ˜ ์ค‘ ํŠน๋ณ„ํ•œ ์„ฑ์งˆ์„ ๊ฐ–๋Š” ๊ฒƒ๋“ค์ด ์žˆ๋Š”๋ฐ, ์ด๋“ค์„ ์ด์นœ์ˆ˜(pinary number)๋ผ ํ•œ๋‹ค. ์ด์นœ์ˆ˜๋Š” ๋‹ค์Œ์˜ ์„ฑ์งˆ์„ ๋งŒ์กฑํ•œ๋‹ค. ์ด์นœ์ˆ˜๋Š” 0์œผ๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด์นœ์ˆ˜์—์„œ๋Š” 1์ด ๋‘ ๋ฒˆ ์—ฐ์†์œผ๋กœ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰, 11์„ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด๋กœ ๊ฐ–์ง€ ์•Š๋Š”๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด 1, 10, 100, 101, 1000, 1001 ๋“ฑ์ด ์ด์นœ์ˆ˜๊ฐ€ ๋œ๋‹ค. ํ•˜์ง€๋งŒ 0010101์ด๋‚˜ 101101์€ ๊ฐ๊ฐ 1, 2๋ฒˆ ๊ทœ์น™์— ์œ„๋ฐฐ๋˜ www.acmicpc.net ์ฝ”๋“œ import java.util.Scanner; public class Main { public static void main(Stri.. 2020. 2. 27.
[๋ฐฑ์ค€] 2966๋ฒˆ: ์ฐ๊ธฐ(์™„์ „ํƒ์ƒ‰, brute force) https://www.acmicpc.net/problem/2966 2966๋ฒˆ: ์ฐ๊ธฐ ๋ฌธ์ œ ์ƒ๊ทผ์ด, ์ฐฝ์˜์ด, ํ˜„์ง„์ด๋Š” ์—ญ์‚ฌ์™€ ์ „ํ†ต์„ ์ž๋ž‘ํ•˜๋Š” Sogang ACM-ICPC Team์— ๊ฐ€์ž…ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ํ•˜์ง€๋งŒ, ๊ฐ€์ž…ํ•˜๋ ค๊ณ  ํ•˜๋Š” ๋ชจ๋“  ์ง€์›์ž๋Š” C์–ธ์–ด ํ•„๊ธฐ์‹œํ—˜์„ ํ†ต๊ณผํ•ด์•ผ ํ•œ๋‹ค. ์ด๋“ค์€ C์–ธ์–ด๋ฅผ ํ•  ์ค„ ๋ชจ๋ฅธ๋‹ค. ๋”ฐ๋ผ์„œ, ํ•„๊ธฐ์‹œํ—˜์„ ๋ชจ๋‘ ์ฐ์œผ๋ ค๊ณ  ํ•œ๋‹ค. ์ƒ๊ทผ์ด๋Š” A, B, C, A, B, C, A, B, C, A, B, C, ...์™€ ๊ฐ™์ด ์ฐ์–ด์•ผ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ํ•˜์ง€๋งŒ, ์ฐฝ์˜์ด๋Š” B, A, B, C, B, A, B, C, B, A, B www.acmicpc.net ์ฝ”๋“œ import java.util.Scanner; public class Main { public static void main(String.. 2020. 2. 26.
[๋ฐฑ์ค€] 1065๋ฒˆ: ํ•œ์ˆ˜(์™„์ „ํƒ์ƒ‰, brute force) https://www.acmicpc.net/problem/1065 1065๋ฒˆ: ํ•œ์ˆ˜ ์–ด๋–ค ์–‘์˜ ์ •์ˆ˜ X์˜ ์ž๋ฆฌ์ˆ˜๊ฐ€ ๋“ฑ์ฐจ์ˆ˜์—ด์„ ์ด๋ฃฌ๋‹ค๋ฉด, ๊ทธ ์ˆ˜๋ฅผ ํ•œ์ˆ˜๋ผ๊ณ  ํ•œ๋‹ค. ๋“ฑ์ฐจ์ˆ˜์—ด์€ ์—ฐ์†๋œ ๋‘ ๊ฐœ์˜ ์ˆ˜์˜ ์ฐจ์ด๊ฐ€ ์ผ์ •ํ•œ ์ˆ˜์—ด์„ ๋งํ•œ๋‹ค. N์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , N๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ํ•œ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. www.acmicpc.net ์ฝ”๋“œ import java.util.Scanner; public class Main { public static boolean check(int num) { boolean flag = false; String str = Integer.toString(num); int first = str.charAt(0) - '0';// ๋ฐฑ์˜์ž๋ฆฌ int mid = s.. 2020. 2. 26.
๋ฐ˜์‘ํ˜•