2010년 10월 29일 금요일

Euler Problem 17

문제 내용
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 the 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.

1부터 19까지는 직접 문자열 길이를 입력하여야 한다. 그리고 20-999까지는  일단위, 십단위, 백단위의 적절한 조합을 통해 문자열 길이를 알아낼 수 있다. 아래 알고리즘은 이 문제를 해결하기 위해 구현한 것이다. 그러나 정갈하게 구성되어 있지 않다.

Sub Euler17()
    Dim OneNineteen
    Dim TenPlace
    OneNineteen = Array("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", _
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen")
    TenPlace = Array("twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety")
   
    Const HundredAnd = 10
    Const onethousand = 11
    Dim i, j, A, total, place1, place2
    Dim Len0_999(0 To 999)   ' 1-99까지의 문자길이 배열, 0은 계산편의상 지정
    Dim LenTenPlace(2 To 9)   ' 20, 30 --- 90까지 문자길이 배열
    i = 2   ' 20-90까지의 문자 길이 배열 저장
    For Each A In TenPlace
        LenTenPlace(i) = Len(A)
        i = i + 1
    Next A
    ' 1-19까지의 문자 길이 배열 저장
    Len0_999(0) = 0 ' 계산편의를 위해 0 지정
    i = 1
    For Each A In OneNineteen
        Len0_999(i) = Len(A)
        i = i + 1
    Next A
    ' 20-99까지 문자 길이 배열 저장
    For i = 20 To 99
        place1 = Len0_999(i - (Int(i / 10) * 10))
        place2 = LenTenPlace(Int(i / 10))
        Len0_999(i) = place1 + place2
    Next
    ' 100-999까지 문자 길이 배열 저장
    For i = 100 To 999
        place1 = Len0_999(i - (Int(i / 100) * 100))
        place2 = Len0_999(Int(i / 100))
        If i Mod 100 = 0 Then
            Len0_999(i) = place2 + 7    ' 7은 hundres 의 문자수
        Else
            Len0_999(i) = place2 + HundredAnd + place1
        End If
    Next
    '합계 결과물 산출 로직
    total = 0
    For i = 1 To 999
        total = total + Len0_999(i)
        ' ActiveCell.Offset(i, 0).Value = total     ' 워크시트에서 결과물 확인
    Next
    Debug.Print total + onethousand     ' 1000의 문자 길이도 합산
End Sub

댓글 없음:

댓글 쓰기

2.1 벡터(Vector)

  R의 자료구조 : 벡터, factor, 행렬, 배열, 데이터프레임, 리스트 벡터(Vector)는 동일한 형태(예, 숫자)의 데이터 구성인자가 1개 이상이면서 1차원으로 구성되어 있는 데이터 구조입니다. w <- c(1, 2, 3, 4, ...