aboutsummaryrefslogtreecommitdiff
path: root/challenge-157/eric-cheung/excel-vba/ch-2.bas
blob: 52a6122b9c1c1ed3e7f1bbb5d106822c54af7653 (plain)
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
Attribute VB_Name = "ModTask_02"
Option Explicit

Function GetBaseSeq(ByVal nNum As Integer, ByVal nBase As Integer) As String
    
    Dim nQuot As Integer, nRemain As Integer
    
    nQuot = nNum
    Do While nQuot >= nBase
        nRemain = nQuot Mod nBase
        GetBaseSeq = nRemain & GetBaseSeq
        nQuot = Int(nQuot / nBase)
    Loop
    GetBaseSeq = nQuot & GetBaseSeq
    
End Function

Function IsSameDigit(ByVal strBaseSeq As String) As Boolean

    Dim nSubLoop As Integer
    Dim strChar As String
    
    strChar = Left(strBaseSeq, 1)
    
    For nSubLoop = 2 To Len(strBaseSeq)
        If strChar <> Mid(strBaseSeq, nSubLoop, 1) Then
            IsSameDigit = False
            Exit Function
        End If
    Next nSubLoop
    
    IsSameDigit = True
    
End Function

Sub Task_02()

    '' Const nInput As Integer = 7 '' Example 1:
    '' Const nInput As Integer = 6 '' Example 2:
    Const nInput As Integer = 8 '' Example 3:

    Dim strMsg As String
    Dim nLoop As Integer
    Dim bFlag As Boolean
    
    bFlag = False
    
    For nLoop = 2 To nInput - 2
        If IsSameDigit(GetBaseSeq(nInput, nLoop)) Then
            bFlag = True
            Exit For
        End If
    Next nLoop
    
    strMsg = "0"
    If bFlag Then
        strMsg = "1"
    End If

    MsgBox strMsg, vbOKOnly, strMyTitle
    
End Sub