잡다한 코딩/Project Euler (Java)

(Java) Project Euler 017 - Number Letter Counts

Skyleester_devNurse 2023. 12. 26. 18:00
// If the numbers 1 to 5 are written out in words: one, two, three, four, five,
// then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

// If all numbers from 1 to 1000(one thousand) inclusive were written out in words,
// how many letters would be used?

// NOTE: Do not count spaces or hyphens.
// For example, 342 (three hundred and forty-two) contains 23 letters and
// 115 (one hundred and fifteen) contains 20 letters.
// The use of "and" when writing out numbers is in compliance with British usage.

 

Question)

-- 1부터 5까지의 숫자를 영어단어로 써보자. : One, Two, Three, Four, Five.

-- 단어의 문자 수는 3 + 3 + 5 + 4 + 4 = 19 이다.

-- 1부터 1000(One thousand)까지의 숫자를 영어단어로 쓰면 몇개의 문자가 사용되는가?

-- 공백과 하이픈(-)은 세지 않는다.

-- 예를들어 342라고 하면, three hundred and forty-two이므로 23문자를 사용한 것이고,

-- 115라고 하면, one hundred and fifteen으로 20문자를 사용한 것이다.

-- and는 영문법에서 사용하는 것을 따라 작성한다.

 

public class ProjectEuler_017 {
    public static void main(String[] args) {
        int letterCount = 11; // "one thousand"

        // 1부터 999까지의 숫자에 대해 글자 수를 계산합니다.
        for (int i = 1; i < 1000; i++) {
            letterCount += GetHundreds(i);
        }

        // 결과를 출력합니다.
        System.out.println("Letter count of words is " + letterCount);
    }

    // 1부터 999까지의 숫자에 대해 백의 자리를 계산하는 메서드
    static int GetHundreds(int num) {
        if (num == 1000) {
            return 11; // "one thousand"
        }

        int H = num / 100; // 백의 자리 숫자
        int under_H = num % 100; // 백의 자리 미만 숫자

        if (H > 0 && under_H > 0) {
            // "and"가 포함된 경우
            return countHundred(H) + countTenth(under_H) + 3; // "and"의 글자 수는 3
        } else {
            // "and"가 없는 경우
            return countHundred(H) + countTenth(under_H);
        }
    }

    // 백의 자리 숫자에 대해 글자 수를 계산하는 메서드
    static int countHundred(int num) {
        if (num > 0) {
            return OneToNine(num) + 7; // "hundred"의 글자 수는 7
        } else {
            return 0;
        }
    }

    // 십의 자리 숫자에 대해 글자 수를 계산하는 메서드
    static int countTenth(int num) {
        int tenthTerm = num / 10; // 십의 자리 숫자
        int underTenth = num % 10; // 십의 자리 미만 숫자

        int count = 0;

        if (tenthTerm > 1) {
            // 십의 자리 숫자가 2 이상인 경우
            count += Tenth(tenthTerm) + OneToNine(underTenth);
        } else if (tenthTerm == 1) {
            // 십의 자리 숫자가 1인 경우
            count += Teen(num);
        } else {
            // 십의 자리 숫자가 0인 경우
            count += OneToNine(num);
        }

        return count;
    }

    // 1부터 9까지의 숫자에 대해 글자 수를 계산하는 메서드
    static int OneToNine(int num) {
        if (num == 1 || num == 2 || num == 6) {
            return 3;
        } else if (num == 4 || num == 5 || num == 9) {
            return 4;
        } else if (num == 3 || num == 7 || num == 8) {
            return 5;
        } else {
            return 0;
        }
    }

    // 10부터 19까지의 숫자에 대해 글자 수를 계산하는 메서드
    static int Teen(int num) {
        if (num == 10) {
            return 3;
        } else if (num == 11 || num == 12) {
            return 6;
        } else if (num == 15 || num == 16) {
            return 7;
        } else if (num == 13 || num == 14 || num == 18 || num == 19) {
            return 8;
        } else if (num == 17) {
            return 9;
        } else {
            return 0;
        }
    }

    // 20, 30, ..., 90까지의 숫자에 대해 글자 수를 계산하는 메서드
    static int Tenth(int num) {
        if (num == 4 || num == 5 || num == 6) {
            return 5;
        } else if (num == 2 || num == 3 || num == 8 || num == 9) {
            return 6;
        } else if (num == 7) {
            return 7;
        } else {
            return 0;
        }
    }
}

 

 

/*
   one         3
   two         3
   three       5
   four        4
   five         4
   six         3
   seven       5
   eight       5
   nine        4
   ten         3
   eleven      6
   twelve      6
   thirteen    8
   fourteen    8
   fifteen      7
   sixteen     7
   seventeen   9
   eighteen    8
   nineteen    8
   twenty      6
   thirty      6
   forty       5
   fifty        5
   sixty       5
   seventy     7
   eighty      6
   ninety      6
   hundred     7
   thousand    8
   and         3
   */