2012年4月6日 星期五

笨小猴

內容:
笨小猴的詞彙量很小,所以每次做英語選擇題的時候都很頭疼。 但是他找到了一種方法,經試驗證明,用這種方法去選擇選項的時候選對的機率非常大!
這種方法的具體描述如下:假設maxn是單詞中出現次數最多的字母的出現次數,minn是單詞中出現次數最少的字母的出現次數,如果maxn-minn是一個質數,那麼笨小猴就認為這是個Lucky Word,這樣的單詞很可能就是正確的答案。
輸入說明:
輸入只有一行,是一個單詞,其中只可能出現小寫字母,並且長度小於100
輸出說明:
輸出共兩行,第一行是一個字符串,假設輸入的的單詞是Lucky Word,那麼輸出“Lucky Word”,否則輸出“No Answer”;
第二行是一個整數,如果輸入單詞是Lucky Word,輸出maxn-minn的值,否則輸出0
範例輸入:
範例1: error
範例2: olymipic
範例輸出:
範例1: Lucky Word 2
範例2: No Answer 0
提示:

單詞error中出現最多的字母r出現了3次,出現次數最少的字母出現了1次,3-1=22是質數。


單詞olymipic中出現最多的字母i出現了2次,出現次數最少的字母出現了1次,2-1=11不是質數。

3 則留言:

  1. Private Sub Form_Load()
    Dim max, min As Integer
    Dim t As Boolean
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Line Input #1, x
    max = 0
    min = 100
    For j = 1 To Len(x)
    b = 0
    For i = 1 To Len(x)
    If Mid(x, j, 1) = Mid(x, i, 1) Then b = b + 1
    Next
    If b > max Then max = b
    If b < min Then min = b
    Next
    c = max - min
    t = True
    For i = 2 To c - 1
    If c Mod i = 0 Then t = False
    Next
    If c = 1 Then t = False
    If t = False Then
    Print #2, "No Answer 0"
    Else
    Print #2, "Lucky Word " & c
    End If
    Loop
    Close
    Close
    End
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As 2
    Dim t As Boolean
    Do Until EOF(1)
    Line Input #1, n
    maxn = 0
    minn = 100
    For i = 1 To Len(n)
    b = 0
    For j = 1 To Len(n)
    x = Mid(n, i, 1)
    y = Mid(n, j, 1)
    If x = y Then b = b + 1
    Next j
    If b > maxn Then maxn = b
    If b < minn Then minn = b
    Next i
    a = maxn - minn
    For i = 2 To a - 1
    If a Mod i = 0 Then t = False
    Next i
    If t = True Then
    Print #2, "Lucky Word" & a
    Else
    Print #2, "No Answer 0"
    End If
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Dim maxn(), minn(), a As Integer
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, x
    tmax = 0: tmin = 1
    ReDim maxn(Len(x)), minn(Len(x))
    For i = 1 To Len(x)
    For j = 1 To Len(x)
    If Mid(x, i, 1) = Mid(x, j, 1) Then maxn(i) = maxn(i) + 1: minn(i) = minn(i) + 1
    Next j
    Next i
    For i = 1 To UBound(maxn())
    For j = 1 To UBound(maxn())
    If maxn(i) >= maxn(j) And maxn(i) >= tmax Then tmax = maxn(i)
    If minn(i) <= minn(j) And maxn(i) <= tmin Then tmin = minn(i)
    Next j
    Next i
    a = tmax - tmin
    If a = 2 Then
    Print #2, "Lucky Word " & a
    Else
    Call b(a)
    End If
    Loop
    Close #2
    Close #1
    End
    End Sub

    Sub b(o)
    If o <= 1 Then Print #2, "No Answer 0": Exit Sub
    For i = 2 To o - 1
    If o Mod i = 0 Then
    Print #2, "No Answer 0"
    Exit For
    End If
    If i = o - 1 Then Print #2, "Lucky Word " & o
    Next i
    End Sub

    回覆刪除