전체 글 (162)
2024-04-03 23:02:20

 

큐를 직접 구현하여, push, pop, size, empty, front, back 메서드를 각각 구현해보는 문제이다

 

 

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
98
99
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 int[] queue = new int[2_000_000];
        static int cnt = 0;
        static int pointer = 0;
        static int tail = 0;
        static void Main(string[] args)
        {
 
 
            int N = int.Parse(Console.ReadLine());
 
            for (int i = 0; i < N; i++)
            {
                menu();
            }
            Console.WriteLine(sb);
        }
        static void menu()
        {
            string[] input = Console.ReadLine().Split();
            string select = input[0];
            int num = -1;
            if (input.Length > 1)
                num = int.Parse(input[1]);
 
            switch (select)
                {
                case "push":
                    Push(num);
                    break;
                case "pop":
                    pop();
                    break;
                case "size":
                    sb.Append(size() + "\n");
                    break;
                case "empty":
                    empty();
                    break;
                case "front":
                    front();
                    break;
                case "back":
                    back();
                    break;
                }
                    
        }
        static void Push(int num)
        {
            queue[tail++= num;
            cnt++;
        }
        static void pop()
        {
            if (cnt == 0)
            { sb.Append(-1 + "\n"); return; }
            sb.Append(queue[pointer+++ "\n");
            cnt--;
        }
        static int size()
        {
            return cnt;
        }
        static void empty()
        {
            if (cnt == 0)
                sb.Append(1 + "\n");
            else
                sb.Append(0 + "\n");
        }
        static void front()
        {
            if (cnt == 0)
                sb.Append(-1 + "\n");
            else
                sb.Append(queue[pointer] + "\n");
        }
        static void back()
        {
            if (cnt == 0)
                sb.Append(-1 + "\n");
            else
                sb.Append(queue[tail-1+ "\n");
        }
    }
}
cs

 

 

저번에 스택을 구현 할 때는 리스트를 이용해서 리스트의 메서드를 가지고 구현을 했어서 아쉬움이 있었다.

그래서 이번에는 배열을 가지고, 대입, 산술 연산만을 이용해서 직접 구현 해 보았다. 근데 Queue는 Dequeue와 Enqueue를 이용하는데 왜 Stack에서 쓰이는 Push, Pop 메서드를 수현하라고 했는지는 의문이다.

2024-04-03 00:50:52

 

승환이가 지금 서있는 줄과, 일방통행만 가능한 줄이 있을 때, 승환이의 차례가 올 수 있는지 구하는 문제이다.

승환이가 서있는 줄은 순서가 무작위로 섞여있는 상태이고,  승환이의 차례는 맨 마지막이다.

 

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
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        //static List<int> stack = new List<int>();
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            int N = int.Parse(reader.ReadLine());
 
            int[] line = Array.ConvertAll(reader.ReadLine().Split(), int.Parse);
 
            Stack<int> stack1 = new Stack<int>();
            Stack<int> stack2 = new Stack<int>();
 
            int cnt = 1;
 
            foreach (int people in line)
            {
                stack1.Push(people);
 
                if (people == cnt)
                {
                    int stacklen = stack1.Count();
                    for (int i = 0; i < stacklen; i++)
                    {
                        if (stack1.Peek() == cnt)
                        {
                            stack2.Push(stack1.Pop());
                            cnt++;
                        }
                    }
                }    
            }
 
            if (stack1.Count() == 0)
                print.WriteLine("Nice");
            else print.WriteLine("Sad");
        }
    }
}
cs

 

승환이가 있는 줄의 사람들을 일단 전부 일방통행만 가능한 줄 (스택1)로 넣는다.

넣는 도중 첫번째 순번의 사람이 오면 그 사람을 간식 받는 줄(스택2)로 보낸다.

그리고 다음 순번의 사람이 스택1의 맨 앞에 있는지 검사하고, 있다면 스택2로 보낸다.

이 부분을 while문으로 무한루프 돌리면서 stack1의 peek가 cnt가 아니라면 break시키는 방법을 사용하면 반복 횟수를 줄일 수 있을것 같다.

아무튼 결과적으로 stack1의 사람이 모두 빠져나갔다면 승환이는 간식을 받은거고, 아니면 못받은 걸로 출력을 해준다.

 

 

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

[백준] 1092번: 배 [C#]  (0) 2024.04.05
[백준] 18258번: 큐 2 [C#]  (0) 2024.04.03
[백준] 10773번: 제로 [C#]  (0) 2024.04.01
[백준] 28278번: 스택 2 [C#]  (0) 2024.04.01
[백준] 13909번: 창문 닫기 [C#]  (0) 2024.04.01
2024-04-02 23:46:28

 

모든 괄호는 짝을 이루어야 한다 () / []

괄호는 서로 다른 괄호끼리 짝을 이룰 수 없다.

 

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
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        //static List<int> stack = new List<int>();
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            while (true)
            {
                string str = Console.ReadLine();
                Stack<char> stack = new Stack<char>();
 
 
                if (str == ".")
                    break;
 
                bool yes = true;
 
                foreach (char s in str)
                {
                    switch (s)
                    {
                        case '(':
                        case '[':
                            {
                                stack.Push(s);
                                break;
                            }
                        case ')':
                            {
                                if (stack.Count() == 0 || stack.Pop() != '(')
                                    yes = false;
                                break;
                            }
                        case ']':
                            {
                                if (stack.Count() == 0 || stack.Pop() != '[')
                                    yes = false;
                                break;
                            }
                    }
                    if (!yes)
                        break;
                }
                
                    if (stack.Count == 0 && yes)
                        sb.Append("yes\n");
                    else
                        sb.Append("no\n");
            }
 
 
            print.WriteLine(sb);
        }
    }
}
cs

 

입력을 한칸씩 돌며 조회해서 여는 괄호 '(', '[' 라면 스택에 추가하고 닫는 괄호 ')' ']' 라면 스택에서 제거한다.

하지만, 스택에서 제거한 요소가 짝이 맞지 않는다면, 짝이 맞지 않음을 bool  타입 변수를 통해 표시하고, 반복문을 종료한다.

스택의 모든 요소가 짝이 맞으면서, 제거되었다면 yes를 출력하고, 아니라면 no를 출력해주었다.

 

2024-04-01 23:23:27

 

N회의 반복을 진행하며, 0이 아닌 정수가 입력되면 저장하고, 0이 입력되면 맨 위의 정수를 제거한다.

모든 입력이 끝난 후, 남은 정수들의 합을 출력한다.

 

 

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
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static List<int> stack = new List<int>();
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            int N = int.Parse(reader.ReadLine());
 
            for (int i = 0; i < N; i++)
            {
                int input = int.Parse(Console.ReadLine());
                if (input == 0)
                    stack.RemoveAt(stack.Count - 1);
                else
                    stack.Add(input);
            }
 
            int count = 0;
            for (int j = 0; j < stack.Count(); j++)
            {
                count += stack[j];
            }
 
            print.WriteLine(count);
 
        }
    }
}
cs

 

스택을 조금 더 파고 들어간 문제인데, 나는 아직 스택을 안배워서 그냥 리스트로 구현했다.

 

 

2024-04-01 22:50:19

 

스택을 직접 구현하여, 스택의 메서드들을 만들어 보는 문제이다.

 

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
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static List<int> stack = new List<int>();
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
 
            int N = int.Parse(reader.ReadLine());
 
            for (int i = 0; i < N; i++)
            {
                int select = -1;
                int num = -1;
                int[] menu = Array.ConvertAll(reader.ReadLine().Split(), int.Parse);
                if (menu[0== 1)
                    add_data(menu[1]);
                else if (menu[0== 2)
                    add_pop();
                else if (menu[0== 3)
                    count_stack();
                else if (menu[0== 4)
                    is_clear();
                else
                    print_top();
            }
            print.WriteLine(sb);
 
        }
 
        static void add_data(int num)
        {
            stack.Add(num);
        }
 
        static void add_pop()
        {
            if (stack.Count == 0)
            {
                sb.Append("-1" + "\n");
                return;
            }
            sb.Append(stack[stack.Count - 1+ "\n");
            stack.RemoveAt(stack.Count - 1);
        }
        static void count_stack()
        {
            sb.Append(stack.Count + "\n");
        }
        static void is_clear()
        {
            if (stack.Count == 0)
                sb.Append(1 + "\n");
            else
                sb.Append(0 + "\n");
        }
        static void print_top()
        {
            if (stack.Count == 0)
            {
                sb.Append(-1 + "\n");
                return;
            }
            sb.Append(stack[stack.Count - 1+ "\n");
        }
    }
}
cs

 

크기가 가변적인 List를 이용해 스택을 구현 해 주었다.

솔직히 구현은 하기는 했는데, 뭔가 메서드 들을 왕창 갖다 써서 좀 그런 거 같다.

나중에 스택을 배우게 되면(한 7~8주차 쯤?) 메서드 보다는 직접 명령문들을 이용해서 구현해 보고 싶다

 

2024-04-01 21:56:01

 

 

정수 N이 입력된다면, 1번부터 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
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());
            StringBuilder sb = new StringBuilder();
 
            int N = int.Parse(reader.ReadLine());
            int cnt = 1;
 
            for (int i = 2; i <= Math.Sqrt(N); i++)
            {
                if (check_square(N))
                    cnt++;
 
            }
            print.WriteLine(cnt);
        }
 
        static bool check_square(int N)
        {
            for (int i = 2; i <= N; i++)
            {
                if ( i*<= N)
                    return true;
            }
            return false;
        }
    }
}
cs

 

 

어떻게 풀어야 할 지 너무 막막해서 일단 예제의 24를 순서대로 진행 해 보았다.

결과적으로 1, 4, 9, 16 이 남게 되었는데, 이 숫자들의 공통점은 제곱으로 된 수 라는 것이다.

이를 이용해서, 최대 21억회 반복해야 할 것을 N의 제곱근 만큼 반복하는 것으로 줄일 수 있었다.

 

2024-03-31 19:01:38

 

골드 바흐 파티션은 한 짝수 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
79
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());
            StringBuilder sb = new StringBuilder();
 
            int T = int.Parse(reader.ReadLine());
            int[] prime_box = new int[1_000_000]; // 소수의 개수를 예측할 수 없어서 임의의 크기를 지정해두었다
            int a = 0; // 소수일 시 하나씩 늘려가며 실제 소수의 개수를 파악한다
 
 
 
            for (int i = 2; i < 1_000_000; i++) // 최대 입력인 1_000_000 까지 소수를 판별해둔다
                if (is_Prime(i))
                    prime_box[a++= i; // 실제 소수의 개수를 얻을 수 있다
 
 
            for (int j = 0; j < T; j++)
            {
                int input = int.Parse(reader.ReadLine());
 
                int count = 0; // 골드바흐 파티션의 개수를 세는 변수
                for (int k = 0; k < a; k++)
                {
                    if (prime_box[k] > input / 2) // 골드바흐 파티션을 중복해서 세는 경우를 방지
                        continue;
                    if (binarySearch(k, input, prime_box, a))
                        count++;
                }
                sb.Append(count + "\n");
            }
 
            print.WriteLine(sb);
 
        }
        static bool binarySearch(int k, int input, int[] prime_box, int a)
        {
            int low = 0;
            int high = a;
 
            while (high >= low)
            {
                int mid = (low + high) / 2;
                if (prime_box[k] + prime_box[mid] == input)
                    return true;
                else if (prime_box[k] + prime_box[mid] > input)
                    high = mid - 1;
                else
                    low = mid + 1;
            }
 
            return false;
        }
 
        static bool is_Prime(int num) // 소수 판별 메서드
        {
            if (num % 2 == 0) // 2를 제외한 짝수는 모두 소수가 아니므로
                return num == 2;
 
            for (int i = 3; i <= Math.Sqrt(num); i+=2) // 소수 판별은 홀수로만 진행한다
            {
                if (num % i == 0)
                    return false;
            }
 
            return true;
        }
    }
}
cs

 

주어질 짝수까지의 소수의 개수를 예측할 수 없어서 임의의 크기의 prime_box 배열을 만들어주었다.

이후 최대로 주어지는 입력인 1_000_000 까지 모든 소수를 판별해서 prime_box에 넣어주었고, 넣을 때마다 변수 a를 1씩 증가시켜 실제 소수의 개수를 파악해 두었다.

골드바흐 파티션의 개수를 세는건 이진 탐색을 이용하여, 시간을 절약해 주었다.

그런데, 예제의 입력 중 6은 서로 중복되는 숫자인 3과 3이 합쳐져 만들어지는 수라 별 이상이 없었지만,

100의 경우 41 - 59와 59 - 41처럼 골드바흐 파티션이 중복되어 카운팅 되는 문제가 있었다.

입력된 짝수의 중간을 기점으로 대칭되어 나타나기 때문에, 입력값의 절반까지만 판정을 진행해 주었다.

 

 

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());
            StringBuilder sb = new StringBuilder();
 
            int T = int.Parse(reader.ReadLine());
            List<int> prime_box = new List<int>();
            int max = 2;
 
            for (int a = 0; a < T; a++)
            {
                int num = int.Parse(reader.ReadLine());
                if (num > max)
                {
                    for (int i = max; i < num; i++)
                    {
                        if (is_prime(i))
                            prime_box.Add(i);
                    }
                    max = num;
 
                }
                int count = 0;
                for (int j = 0; j < prime_box.Count()-1; j++)
                {
                    if (prime_box[j] > num/2)
                        continue;
                    if (binary_search(prime_box, num, j))
                        count++;
                } sb.Append(count + "\n");
            }
            print.WriteLine(sb);
        }
 
        static bool binary_search(List<int> prime_box, int a, int j)
        {
            int low = 0;
            int high = prime_box.Count() -1;
 
            while (high >= low)
            {
                int mid = (low + high) / 2;
                if (prime_box[mid] + prime_box[j] == a)
                    return true;
                else if (prime_box[mid] + prime_box[j] > a)
                    high = mid - 1;
                else
                    low = mid + 1;
            }
            return false;
        }
 
        static bool is_prime(int input)
        {
            if (input % 2 == 0)
                return input == 2;
 
            for (int i = 3; i <= Math.Sqrt(input); i+=2)
            {
                if (input % i == 0)
                    return false;
            }
 
            return true;
        }
    }
}
cs

 

소수의 개수를 예측할 수 없는 문제 때문에 배열에 너무나 큰 값을 줘버려, 메모리 낭비라고 생각이 들었다.

리스트는 검색 속도는 느리지만, 가변적으로 변하는 크기 덕분에 버려지는 공간이 없어서 이렇게 코드를 짜 보았다.

확실히 리스트를 사용하니 따로 소수의 크기를 저장해둘 변수를 쓸 필요가 없어져 더우 직관적으고 효율적인 코드가 된 것 같다.

2024-03-31 00:17:53

 

n부터 2n까지의 소수의 개수를 찾아서 출력해 주면 된다.

 

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
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());
            StringBuilder sb = new StringBuilder();
 
            while (true)
            {
                int input = int.Parse(reader.ReadLine());
                if (input == 0)
                    break;
                int cnt = 0;
 
                for (int i = input+1; i <= input * 2; i++)
                {
 
                    if (is_Prime(i))
                        cnt++;
                }
                sb.Append(cnt + "\n");
            }
            print.WriteLine(sb);
        }
 
        static bool is_Prime(int num)
        {
            if (num <= 1)
                return false;
 
            for (int i = 2; i <= Math.Sqrt(num); i++)
            {
                if (num % i == 0)
                    return false;
            }
 
            return true;
        }
    }
}
cs

 

전 문제와 별 차이가 없다. 문자열 문제에서 나오는 특정 값이 나오면 종료하는 부분만 차이가 있는 거 같음..

 

 

2024-03-30 23:52:51

 

두 수 사이의 소수들을 몽땅 구해다가 출력해주면 된다.

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
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());
            StringBuilder sb = new StringBuilder();
 
            int[] input = Array.ConvertAll(reader.ReadLine().Split(), int.Parse);
            int a = Math.Min(input[0], input[1]);
            int b = Math.Max(input[0], input[1]);
 
            for (int i = a; i <= b; i++)
            {
                if (is_Prime(i))
                    sb.Append(i + "\n");
            }
            print.WriteLine(sb);
        }
 
        static bool is_Prime(int num)
        {
            if (num <= 1)
                return false;
 
            for (int i = 2; i <= Math.Sqrt(num); i++)
            {
                if (num % i == 0)
                    return false;
            }
 
            return true;
        }
    }
}
cs

2024-03-30 23:25:04

 

int형 만으로는 처리할 수 없는 큰 수가 입력으로 주어진다.

주어진 값을 소수인지 판별하고, 소수가 아니라면 가장 가까운 소수를 출력해주면 된다.

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
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());
            StringBuilder sb = new StringBuilder();
 
            int T = int.Parse(reader.ReadLine());
 
            for (long i = 0; i < T; i++)
            {
                long num = long.Parse(reader.ReadLine());
                while (is_Prime(num))
                {
                    num++;
                }
                sb.Append(num + "\n");
            }
            print.WriteLine(sb);
        }
 
        static bool is_Prime(long num)
        {
            if (num <= 1)
            {
                return true;
            }
            bool is_prime = false;
 
            for (long i = 2; i < Math.Sqrt(num); i++)
            {
                if (num % i == 0)
                {
                    is_prime = true;
                    break;
                }
            }
 
            return is_prime;
        }
    }
}
cs

 

 처음에 제출했을때 남색 글씨로 '런타임 에러' 가 떴는데, 당연히 시간초과겠구나 생각해서 제대로 읽어보지도 않고 시간을 줄일 방법을 고민했었다. 그런데, 이미 소수 찾는 과정은 엄청나게 시간을 줄여놓았으니, 수정할 부분은 가장 가까운  소수를 찾는 부분이라고 생각했다. 그래서 대안인 '에라토스테네스의 체' 라는 방법을 사용해보려 했다.

그런데, 주어지는 입력이 너무나 커서 미리 계산해놓는 방법이 더 손해인거 같았다. 혹시 몰라 다시 제출 내용을 봤더니

 '런타임 에러(Overflow)' 였다;;;

생각해보니 주어지는 값이 너무 커서 21억 정도까지 밖에 표현을 못하는 int형으로는 당연히 오버플로우가 나는 것 이었다.

그래서 모든 변수를 long형으로 바꿔 주었는데 또 오버 플로우가 뜨는것이었다;;;;

너무 당황스러워서 뭐지 하고 있었는데... 알고보니  long형 변수를 선언해두곤 정작 입력을 받을 때에는 int형으로 받는 찐빠를 저질러 놓았던 거였다..

바보