SARACEN's Blog
  • [C언어]Project Euler(프로젝트 오일러) 4 번
    2020년 08월 03일 22시 11분 46초에 업로드 된 글입니다.
    작성자: RACENI
    ::문제::
    앞에서부터 읽을 때나 뒤에서부터 읽을 때나 모양이 같은 수를 대칭수(palindrome)라고 부릅니다.
    두 자리 수를 곱해 만들 수 있는 대칭수 중 가장 큰 수는 9009 (= 91 × 99) 입니다.
    세 자리 수를 곱해 만들 수 있는 가장 큰 대칭수는 얼마입니까?

    ::문제 주소::
    https://euler.synap.co.kr/problem=4

     

    -이하 소스 코드-

    #include <stdio.h>
    #include <stdbool.h>
    
    int main()
    {
        int max = 0;
        for (int i = 100; i < 1000; i++)
        {
            for (int j = 100; j < 1000; j++)
            {
                int pail = i * j;
                char chpail[6];
    
                sprintf(chpail, "%d", pail);
    
                pail = i * j;
    
                bool check = true;
                
                for (int k = 0; ; k++)
                {
                    if (chpail[k] != chpail[sizeof(chpail) / sizeof(char) - k- 1])
                    {
                        check = false;
                    }
                    if(k == 2)
                    {
                        break;
                    }
                }
                if(check && pail > max)
                {
                    max = pail;
                }
            }
        }
        printf("%d\n", max); // 906609
    
        return 0;
    }
    댓글