카테고리 없음

[백준] 1436 영화감독 숌 [C#]

NaZZU 2024. 3. 25. 00:06

악마의 수는 666이 들어있는 숫자를 말한다고 한다.

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
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
 
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());
 
            string num = "666";
            int count = 0;
 
            while(count != N)
            {
                for(int i = 0; i+2 < num.Length; i++)
                {
 
                    if (num[i] == '6' && num[i + 1== '6' && num[i + 2== '6')
                    {
                        count++;
                        if (count == N)
                            sb.Append(num);
                        break;
                    }
                }
                num = (int.Parse(num) + 1).ToString();
            }
 
            print.WriteLine(sb);
            
        }
    }
}
cs

 

우선, 몇 번째 악마의 수를 찾을 것인지 입력받는다.

이후 루프를 돌며 이번 숫자의 n번째, n+1번째, n+2 번째가 6인지 체크한다.

만약 6이 3번 연속되어 나타난다면, 카운트를 하나 올려서 몇 번째 악마의 수를 찾은 것인지 저장한다.

이후 N과 카운트 횟수가 같다면 해당 악마의 수를 출력하고, 루프를 종료한다.

while문의 경우 카운트 횟수가 N보다 클 경우 종료되기 때문에 따로 break문을 쓰지 않아도 된다.