전체 글 (165)
2024-05-14 00:59:44

 

 

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            _2164_boj boj = new _2164_boj();
 
            boj.boj_2164();
 
        }
    }
        internal class _2164_boj
    {
        static int n;
        static int front;
        static int rear;
        static string[] queue;
        public void boj_2164()
        {
            n = int.Parse(Console.ReadLine());
 
            queue = new string[n + 1];
 
            front = 0;
            rear = 0;
 
            for (int i = 1; i <= n; i++)
                enqueue(i);
 
            int cnt = n;
            while (true)
            {
                if (cnt == 1)
                    break;
                dequeue();
                cnt--;
                enqueue(int.Parse(dequeue()));
            }
 
            Console.WriteLine(dequeue());
        }
        public bool isQueueFull()
        {
            if ((rear + 1) % (n + 1== front)
                return true;
            else
                return false;
        }
        public bool isQueueEmpty()
        {
            if (rear == front)
                return true;
            else
                return false;
        }
        public void enqueue(int data)
        {
            if (isQueueFull())
                return;
            rear = (rear + 1) % (n + 1);
            queue[rear] = data.ToString();
        }
        public string dequeue()
        {
            string data;
            if (isQueueEmpty())
                return null;
            front = (front + 1) % (n + 1);
            data = queue[front];
            queue[front] = null;
            return data;
        }
    }
}
 
 
cs

 

수업 시간에 큐를 배운만큼 큐의 메서드들을 직접 구현해 보았다.

문제가 덱으로 풀면 참 쉽게 풀릴거 같은데 나는 덱을 안배웠으니 내가 배운 원형 큐를 이용해서 풀어보았다.

 

2024-05-13 23:29:08

주어진 알파벳들중 적어도 모음 1개, 자음 2개를 포함하는 n길이의 문자열을 사전 순으로 모두 구하는 문제이다.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            _1759_boj boj = new _1759_boj();
 
            boj.boj_1759();
 
        }
    }
        internal class _1759_boj
    {
        static StringBuilder sb = new StringBuilder();
        static int[] arr;
        static string[] input;
        static List<string> temp;
        static int ja = 0;
        static int mo = 0;
        public void boj_1759()
        {
            
            arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
 
            input = Console.ReadLine().ToLower().Split();
            Array.Sort(input);
 
            temp = new List<string>();
 
            makeCombi(00);
 
            if (sb[sb.Length - 1== '\n')
            {
                sb.Remove(sb.Length - 11);
            }
            Console.Write(sb);
        } // a c i s t w
        public void makeCombi(int depth, int breadth )
        {
            for (int j = depth + breadth ; j <= arr[1- (arr[0- depth); j++)
            {
                temp.Add(input[j]);
                if (temp[temp.Count() - 1== "a" || temp[temp.Count() - 1== "e" || temp[temp.Count() - 1== "i" || temp[temp.Count() - 1== "o" || temp[temp.Count() - 1== "u")
                    mo++;
                else
                    ja++;
 
                if (temp.Count() == arr[0&& (mo >= 1 && ja >= 2))
                {
                    foreach (var _ in temp)
                        sb.Append(_);
                    sb.Append('\n');
                }
                if (temp.Count() < arr[0])
                    makeCombi(depth + 1, breadth);
 
                if (temp[temp.Count() - 1== "a" || temp[temp.Count() - 1== "e" || temp[temp.Count() - 1== "i" || temp[temp.Count() - 1== "o" || temp[temp.Count() - 1== "u")
                    mo--;
                else
                    ja--;
                temp.RemoveAt(temp.Count() - 1);
               breadth++;
            }
        }
    }
}
 
 
cs

 

처음 봤을 때엔 쉬운 문제라고 생각이 들었었다. 하지만 막상 문제를 풀어보니 생각보다 복잡한 문제였다.

일단 출력 조건이 문자열을 사전순으로 출력하는 것이었다. 어차피 문자열을 사전순으로 만들면 출력도 사전순으로 될 테니 입력받은 문자열을 정렬해주는 것으로 대신하였다.

 

문자열을 만드는 함수를 재귀함수로 만드는 것이 꽤나 힘들었다.

첫번째 문자는 a, c. i가 올 수 있고, 두번쨰 문자는 c, i, s가 올 수 있었는데, 시작점은 변동되지만 (첫번째 문자는 0, 1, 2번 인덱스, 두번째 문자는 1, 2, 3번째 인덱스) 종료점은 불변 (첫번째 문자는 2번 인덱스 까지, 두번째 문자는 3번 인덱스 까지) 한 특징을 구현하기 위해 깊이와 너비의 개념을 떠올렸다.

 

문자열이 완성될때 까지 깊이를 증가시켜준다. 문자열이 완성되면, 이후에는 너비를 증가시켜주며 종료점까지 문자열을 만들어준다.

 

모든 문자열이 만들어지면, 마지막줄에 있는 빈줄을 삭제시켜 주며 출력을 했다.

 

 

'하루 한 접시' 카테고리의 다른 글

[백준] 요세푸스 문제 0 [C#]  (0) 2024.05.14
[백준] 2164번: 카드2 [C#]  (0) 2024.05.14
[백준] 10828번: 스택 [C#]  (0) 2024.05.12
[백준] 8979번: 올림픽 [C#]  (0) 2024.05.08
[백준] 1924번: 2007년 [C#]  (0) 2024.05.07
2024-05-12 01:31:46

 

 

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            _10828_boj boj = new _10828_boj();
 
            boj.boj_10828();
 
        }
    }
        internal class _10828_boj
    {
        public void boj_10828()
        {
            StreamReader reader = new StreamReader(Console.OpenStandardInput());
            StringBuilder sb = new StringBuilder();
 
            int n = int.Parse(reader.ReadLine());
 
            List<string> stack = new List<string>();
            int top = -1;
 
            string[] input = new string[2];
 
            for (int i = 0; i < n; i++)
            {
                input = reader.ReadLine().Split();
 
                switch (input[0])
                {
                    case "push":
                        top += 1;
                        stack.Add(input[1]);
                        break;
                    case "pop":
                        if (top > -1)
                        {
                            sb.Append(stack[top]+"\n");
                            stack.RemoveAt(top);
                            top -= 1;
                        }
                        else
                            sb.Append("-1\n");
                        break;
                    case "size":
                        sb.Append((top + 1+ "\n");
                        break;
                    case "empty":
                        sb.Append(top > -1 ? "0\n" : "1\n");
                        break;
                    case "top":
                        sb.Append(top > -1 ? stack[top]+"\n" : "-1\n");
                        break;
 
                }
            }
            if (sb.Length > 0 && sb[sb.Length - 1== '\n')
                sb.Remove(sb.Length - 11);
            Console.WriteLine(sb);
        }
    }
}
 
 
cs

이 쉬운걸 풀겠다고 몇번을 틀린건지...

 

'하루 한 접시' 카테고리의 다른 글

[백준] 2164번: 카드2 [C#]  (0) 2024.05.14
[백준] 1759번: 암호 만들기 [C#]  (0) 2024.05.13
[백준] 8979번: 올림픽 [C#]  (0) 2024.05.08
[백준] 1924번: 2007년 [C#]  (0) 2024.05.07
[백준] 2816번: 디지털 티비 [C#]  (0) 2024.05.06
2024-05-10 23:40:43

 

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
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            _11047_boj boj = new _11047_boj();
 
            boj.boj_11047();
 
        }
    }
        internal class _11047_boj
    {
        public void boj_11047()
        {
            int[] input = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
 
            int[] arr = new int[input[0]];
 
            for (int i = 0; i < input[0]; i++)
            {
                arr[i] = int.Parse(Console.ReadLine());
            }
 
            int[] dp = new int[input[1+ 1];
            dp[0= 0;
 
            for (int i = 0; i < input[0]; i++)
            {
                for (int j = arr[i]; j <= input[1]; j++)
                {
                    dp[j] = 1 + dp[j - arr[i]];
                }
            }
            Console.WriteLine(dp[input[1]]);
        }
    }
}
 
cs

 

그리디는 안 읶뜎하니깐 다이나믹 프로그래밍으로 풀었다

 

2024-05-09 23:32:58

 

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
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            _2747_boj boj = new _2747_boj();
 
            boj.boj_2747();
 
        }
    }
        internal class _2747_boj
    {
        public void boj_2747()
        {
            int n = int.Parse(Console.ReadLine());
 
            long[] dp = new long[n + 1];
            dp[0= 0;
            if (n >= 1)
                dp[1= 1;
            if (n >= 2)
                dp[2= 1;
            if (n <= 2){
                Console.WriteLine(dp[n]);
                return;
            }
 
            for (int i = 3; i <= n; i++)
            {
                dp[i] = dp[i - 2+ dp[i - 1];
            }
            Console.WriteLine(dp[n]);
        }
    }
}
 
cs

 

 

2024-05-08 00:53:38

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            _8979_boj boj = new _8979_boj();
 
            boj.boj_8979();
 
        }
    }
        internal class _8979_boj
    {
        public void boj_8979()
        {
            int[] input = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            int cnt = input[0];
            int obj = input[1];
 
            int[][] countries = new int[cnt][];
 
            for (int i = 0; i < cnt; i++)
            {
                countries[i] = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            }
            int[] temp;
            for (int i = 0; i < cnt; i++)
            {
                for (int j = i + 1; j < cnt; j++)
                {
                    for (int k = 1; k < 4; k++)
                    {
                        if (countries[i][k] == countries[j][k])
                        {
                            if (k == 4)
                            {
                                temp = countries[i + 1];
                                countries[i + 1= countries[j];
                                countries[j] = temp;
                                break;
                            }
                            continue;
                        }
                        if (countries[i][k] < countries[j][k])
                        {
                            temp = countries[i];
                            countries[i] = countries[j];
                            countries[j] = temp;
                            break;
                        }
                        if (countries[i][k] > countries[j][k])
                            break;
                    }
                }
            }
 
            int strick = 1;
            for (int i = 1; i <= cnt; i++)
            {
                if (countries[i - 1][0== obj)
                {
                    if (i > 2 && countries[i - 2][1== countries[i - 1][1&& countries[i - 2][2== countries[i - 1][2&& countries[i - 2][3== countries[i - 1][3])
                    {
                        for (int j = i - 2; j >= 0; j--)
                        {
                            if (countries[i - 1][1!= countries[j][1|| countries[i - 1][2!= countries[j][2|| countries[i - 1][3!= countries[j][3])
                            {
                                Console.WriteLine(j + 2);
                                return;
                            }
                        }
                    }
                    Console.WriteLine(i);
                    return;
 
                }
            }
        }
    }
}
 
cs

 

코드를 더 이쁘게 짜는 방법이 있을텐데.... 너무 안예쁜 코드다...

 

 

2024-05-07 23:50:40

 

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
54
55
56
57
58
59
60
61
62
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            _1924_boj boj = new _1924_boj();
 
            boj.boj_1924();
 
        }
    }
        internal class _1924_boj
    {
        public void boj_1924()
        {
            int[] Months = { 312831303130313130313031 };
            int[] input = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
 
            int days = input[1];
            for (int i = 0; i < input[0- 1; i++)
                days += Months[i];
 
            days %= 7;
            switch(days)
            {
                case 0:
                    Console.WriteLine("SUN");
                    break;
                case 1:
                    Console.WriteLine("MON");
                    break;
                case 2:
                    Console.WriteLine("TUE");
                    break;
                case 3:
                    Console.WriteLine("WED");
                    break;
                case 4:
                    Console.WriteLine("THU");
                    break;
                case 5:
                    Console.WriteLine("FRI");
                    break;
                case 6:
                    Console.WriteLine("SAT");
                    break;
            }
        }
    }
}
 
cs
2024-05-06 17:57:58

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            _2816_boj boj = new _2816_boj();
 
            boj.boj_2816();
 
        }
    }
    internal class _2816_boj
    {
        public void boj_2816()
        {
            int n = int.Parse(Console.ReadLine());
            string[] TV = new string[n];
 
            for (int i =0; i < n; i++)
                TV[i] = Console.ReadLine();
 
            List<int> ctrl = new List<int>();
            int pointer = 0;
            string temp;
            string[] KBS = { "KBS1""KBS2" }; // 코드 재활용을 위해 값이 들어있는 배열 생성
            for (int i = 0; i < 2; i++)
            {
                while (true)
                {
                    if (TV[i] == KBS[i]) // 올바르게 정렬 시 종료
                        break;
                    if (TV[i] != KBS[i]) // 먼저 KBS1을 첫번째 자리로 이동
                    {
                        if (i > 0 && TV[pointer] == KBS[i - 1])
                        {
                            ctrl.Add(1);
                            pointer++;
                            continue;
                        }
                        if (TV[pointer] == KBS[i]) // 현재 칸이 KBS1이라면
                        {
                            temp = TV[pointer];
                            TV[pointer] = TV[pointer - 1];
                            TV[pointer - 1= temp;
                            ctrl.Add(4); // 4번 연산 수행
                            pointer--;
                            continue;
                        }
                        if (pointer == i && TV[pointer + 1== KBS[i]) // KSB1이 1번에 위치한다면
                        {
                            temp = TV[pointer];
                            TV[pointer] = TV[pointer + 1];
                            TV[pointer + 1= temp;
                            ctrl.Add(3); // 3번 연산 수행
                            pointer++;
                            continue;
                        }
 
                        if (TV[pointer + 1!= KBS[i]) // 포인터가 가리키는 칸의 아래가 KBS1이 아니라면
                        {
                            temp = TV[pointer];
                            TV[pointer] = TV[pointer + 1];
                            TV[pointer + 1= temp;
                            ctrl.Add(3); // 3번 연산 수행
                            pointer++;
                            continue;
                        }
                        if (TV[pointer + 1== KBS[i]) // 아래칸이 KBS1이라면
                        {
                            pointer++;
                            ctrl.Add(1); // 1번 연산 수행
                            continue;
                        }
                    }
                }
            }
            foreach (var _ in ctrl)
                Console.Write(_);
 
        }
    }
}
 
cs

브론즈 문제지만 주어진 조건을 모두 사용한다면 꽤나 생각해 볼만한 것들이 많았던 문제였다. 맛있는 문제!

처음에는 while문 안에 두개의 분기를 만들어 (분기 1: 아직 KBS1을 맨 위 칸으로 불러오자, 분기 2: KBS2를 다음 칸으로 불러오자)

해주려 했지만 너무 코드의 길이가 길어지는것 같아 KBS1/ 2가 담긴 배열에서 값을 불러와서 사용하는 방향으로 틀어주었다.

기본 구조는 아래칸이 찾으려 하는 방송국 (KBS1 /2)가 아닐 때 3번 연산을 진행해 위치를 교환하고,

아래칸이 찾으려는 방송국이면 1번 연산을 진행해 해당 방송국으로 포인터를 옮기고, 해당 방송국을 알맞는 위치 (KBS1은 0으로, KBS2는 1로) 4번 연산을 통해 이동해준다.

예외사항으로 해당 포인터가 해당 방송국의 목표위치에 있으며, 아래칸에 해당 방송국이 있다면 3번 연산을 진행하며,

KBS1을 4번 연산을 진행해서 목표 위치로 가져왔을경우 기본연산인 3번연산을 진행하면 안되기 때문에, 1번 연산을 진행해 주었다.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            _2816_boj boj = new _2816_boj();
 
            boj.boj_2816();
 
        }
    }
        internal class _2816_boj
    {
        public void boj_2816()
        {
            int n = int.Parse(Console.ReadLine());
            string[] TV = new string[n];
 
            for (int i =0; i < n; i++)
                TV[i] = Console.ReadLine();
 
            List<int> ctrl = new List<int>();
            int pointer = 0;
            while (true)
            {
                if (TV[0== "KBS1" && TV[1== "KBS2")
                    break;
                if (TV[0!= "KBS1")
                {
                    if (TV[pointer] != "KBS1")
                    {
                        ctrl.Add(1);
                        pointer++;
                    }
                    if (TV[pointer] == "KBS1")
                    {
                        string temp;
                        temp = TV[pointer];
                        TV[pointer] = TV[pointer - 1];
                        TV[pointer - 1= temp;
                        ctrl.Add(4);
                        pointer--;
                    }
                }
                if (TV[1!= "KBS2" && TV[0== "KBS1")
                {
                    if (TV[pointer] != "KBS2")
                    {
                        ctrl.Add(1);
                        pointer++;
                    }
                    if (TV[pointer] == "KBS2")
                    {
                        string temp;
                        temp = TV[pointer];
                        TV[pointer] = TV[pointer - 1];
                        TV[pointer - 1= temp;
                        ctrl.Add(4);
                        pointer--;
                    }
                }
            }
            foreach (var _ in ctrl)
                Console.Write(_);
        }
    }
}
 
cs

이건 첫번째에 짰던 코드인데 설마 싶어서 마지막에 출력해주는 것을 WriteLine에서 Write 로 바꿔주니 맞았다....

브론즈 문제 맞네... 맞어...

 

 

2024-05-05 02:18:32

https://www.acmicpc.net/board/view/64912

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            _2621_boj boj = new _2621_boj();
 
            boj.boj_2621();
 
        }
    }
    internal class _2621_boj
    {
        public void boj_2621()
        {
            int[] color = new int[4];
            int[] num = new int[10];
            int max = 0;
            int[] pair = new int[2];
            int Triple = 0;
            int Four = 0;
            bool flush = false;
            bool straight = false;
 
            for (int i =0; i < 5; i++)
            {
                string[] input = Console.ReadLine().Split();
                switch (input[0])
                {
                    case "R":
                        color[0]++;
                        break;
                    case "B":
                        color[1]++;
                        break;
                    case "Y":
                        color[2]++;
                        break;
                    default:
                        color[3]++;
                        break;
                }
                num[int.Parse(input[1])]++;
                max = Math.Max(max, int.Parse(input[1]));
            }
            int a = 0;
            for (int i = 1; i<= 9; i++)
            {
                
                if (num[i] == 2)
                    pair[a++= i;
                if (num[i] == 3)
                    Triple = i;
                if (num[i] == 4)
                    Four = i;
            }
 
            if (color[0== 5 || color[1== 5 || color[2== 5 || color[3== 5)
                flush = true;
            for (int i = 0; i < 6; i++)
                if ((num[i] != 0&& (num[i + 1!= 0&& (num[i + 2!= 0&& (num[i + 3!= 0&& (num[i + 4!= 0))
                    straight = true;
 
            if (flush && straight)
                Console.WriteLine(max + 900);
            else if (Four != 0)
                Console.WriteLine(Four + 800);
            else if (Triple != 0 && pair[0!= 0)
                Console.WriteLine(Triple * 10 + (pair[0+ 700));
            else if (flush)
                Console.WriteLine(max + 600);
            else if (straight)
                Console.WriteLine(max + 500);
            else if (Triple != 0)
                Console.WriteLine(Triple + 400);
            else if (pair[0!= 0 && pair[1!= 0)
                Console.WriteLine(300 + Math.Max(pair[0], pair[1]) * 10 + Math.Min(pair[0], pair[1]));
            else if (pair[0!= 0)
                Console.WriteLine(pair[0+ 200);
            else
                Console.WriteLine(max + 100);
 
        }
    }
}
 
cs

단순한 문제였지만 뭐 대단한 문제인거 마냥 메서드를 분할해서 해보려다 코드가 너무 길어져서 다시 하나의 메서드로 통합해서 만들어 주었다.

 

2024-05-04 23:56:44

 예제) 

. . X . .
. . X . .
XXXXX
. . X . .
. . X . .

이 경우 누울 장소가 8 8로 주어져야 한다

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            _1652_boj boj = new _1652_boj();
 
            boj.boj_1652();
 
        }
    }
    internal class _1652_boj
    {
        public void boj_1652()
        {
            int n = int.Parse(Console.ReadLine());
            int x = 0, y = 0;
 
            string[] room = new string[n];
 
            for (int i =0; i < n; i++)
                room[i] = Console.ReadLine();
 
            for (int i = 0; i < n; i++)
            {
                int space = 0;
                for (int j = 0; j < n; j++)
                {
                    if (room[i][j] == '.')
                    {
                        space++;
                        if (space == 2)
                            x++;
                    }
                    else
                        space = 0;
                }
            }
            for (int i = 0; i < n; i++)
            {
                int space = 0;
                for (int j = 0; j < n; j++)
                {
                    if (room[j][i] == '.')
                    {
                        space++;
                        if (space == 2)
                            y++;
                    }
                    else
                        space = 0;
                }
            }
            Console.WriteLine(x + " " + y);
        }
    }
}
 
cs

문제 자체가 한번에 이해할 수 있기 보다는 깊게 생각을 해보고 예외적인 상황을 생각해봐야하는 문제인것 같다.