2012年11月5日 星期一

授權碼檢查

Nanosoft資訊軟體公司研發出一套商用軟體,準備於市面上販售,但唯恐遭到未經授權之不合法使用,因此在安裝軟體時,需要使用者輸入授權馬。試寫一個程式,其功能可以檢核此授權碼的正確信。
【規則敘述】
此授權碼由十個位數合成,每一位數可以為0~9的任何一個數字,是判斷方法如下:
首先,將此碼逐次的累加,使的第二位數成為第一位數到第二位數的和,第三位數為第一位數到第三位數的和....第十位數為第一位數到第十位數的和;
進行完第一次的累加和後 ,接著再將所得到的十個數字,第十個數字減掉第一個數字,第九個數字減掉第二個數字....以此類推,得到五組數字,將此五組數字由小到大排列後成為一個新的數列。
此新數列的後三碼為識別碼,必須可為11整除,方為正確的授權碼,故在螢幕上列印出"此為合法之授權碼",反之則印出"此為不合法之授權碼",若授權碼格式錯誤則顯示"授權碼格式錯誤"。

輸入範例:
請輸入授權碼:9476282354
輸出範例:
此為合法之授權碼

【提示】
輸入授權碼為9476282354
第一次運算後得到數列 9 13 20 26 28 36 38 41 46 50
第二次運算後得到數列 41 33 21 12 8
排序後得到數列引 812213341 (341被11整除)

5 則留言:

  1. Dim num, num1(9), num2(4), ok As Double
    Dim strr As String
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, num
    For i = 0 To 9
    For ii = 1 To i + 1
    num1(i) = Val(num1(i)) + Val(Mid(num, ii, 1))
    Next
    Next

    mycount = 0
    For ai = 9 To 5 Step -1
    num2(mycount) = num1(ai) - num1(9 - ai)
    mycount = mycount + 1
    Next

    For bi = 4 To 0 Step -1
    strr = strr & Str(num2(bi))
    strr = Replace(strr, " ", "")
    ok = Val(Right(strr, 3))
    Next
    If ok Mod 11 = 0 Then
    Print #2, "合法之授權碼"
    Else
    Print #2, "不合法之授權碼"
    End If
    Close #2
    Close #1
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Dim key(9), Re(9) As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, num
    For i = 0 To 9
    key(i) = Mid(num, i + 1, 1)
    Next
    Re(0) = key(0)
    For j = 1 To 9
    Re(j) = key(j) + Re(j - 1)
    'Print #2, Re(j)
    Next
    For k = 0 To 4
    key(k) = Re(9 - k) - Re(k)
    Next
    For z = 0 To 4
    For g = 0 To 3
    If key(g) > key(g + 1) Then
    a = key(g)
    key(g) = key(g + 1)
    key(g + 1) = a
    End If
    Next
    Next
    For q = 0 To 4
    s = s & key(q)
    Next
    'Print #2, s
    ans = Right(s, 3)
    'Print #2, ans
    If ans Mod 11 = 0 Then
    Print #2, "此為合法之授權碼"
    Else
    Print #2, "此為不合法之授權碼"
    End If
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. 題目要看仔細
    "若授權碼格式錯誤則顯示"授權碼格式錯誤"。"

    回覆刪除
  4. Dim s As Boolean
    Dim z As Boolean
    Dim y As Boolean
    Dim a(10), b(10), c(5) 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
    Input #1, x
    s = False
    z = False
    y = False
    For i = 1 To 10
    q = Mid(x, i, 1)
    For k = 0 To 9
    If Not q = k Then
    y = False
    Else
    y = True
    End If
    Next
    Next
    If Len(x) = 10 Then
    z = True
    For i = 1 To 10
    a(i) = Val(Mid(x, i, 1))
    Next
    For i = 1 To 10
    For j = 1 To i
    b(i) = b(i) + a(j)
    Next
    Next
    d = 10
    For i = 1 To 5
    c(i) = b(d) - b(i)
    d = d - 1
    Next
    For i = 1 To 5
    For j = 1 To 4
    If c(j) > c(j + 1) Then
    x = c(j)
    c(j) = c(j + 1)
    c(j + 1) = x
    End If
    Next

    Next
    For i = 1 To 5
    t = t & c(i)
    Next
    If Val(Right(t, 3)) Mod 11 = 0 Then
    s = True
    Else
    s = False
    End If
    Else
    z = False
    End If

    If s = True And z = True And y = True Then
    Print #2, "此為合法之授權碼"
    ElseIf s = False And z = True And y = True Then
    Print #2, "此為不合法之授權碼"
    ElseIf z = False Or y = False Then
    Print #2, "授權碼格式錯誤"
    End If
    Close
    Close
    End
    End Sub

    回覆刪除
  5. kiki錯誤更正如下 (我只加註解就可以正確了)

    Dim s As Boolean
    Dim z As Boolean
    'Dim y As Boolean
    Dim a(10), b(10), c(5) 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
    Input #1, x
    s = False
    z = False
    'y = False
    'For i = 1 To 10
    'q = Mid(x, i, 1)
    'For k = 0 To 9
    'If Not q = k Then
    'y = False
    'Else
    'y = True
    'End If
    'Next
    'Next
    If Len(x) = 10 Then
    z = True
    For i = 1 To 10
    a(i) = Val(Mid(x, i, 1))
    Next
    For i = 1 To 10
    For j = 1 To i
    b(i) = b(i) + a(j)
    Next
    Next
    d = 10
    For i = 1 To 5
    c(i) = b(d) - b(i)
    d = d - 1
    Next
    For i = 1 To 5
    For j = 1 To 4
    If c(j) > c(j + 1) Then
    x = c(j)
    c(j) = c(j + 1)
    c(j + 1) = x
    End If
    Next

    Next
    For i = 1 To 5
    t = t & c(i)
    Next
    If Val(Right(t, 3)) Mod 11 = 0 Then
    s = True
    Else
    s = False
    End If
    'Else
    'z = False
    End If

    If s = True And z = True Then 'And y = True
    Print #2, "此為合法之授權碼"
    ElseIf s = False And z = True Then 'And y = True
    Print #2, "此為不合法之授權碼"
    ElseIf z = False Then 'Or y = False
    Print #2, "授權碼格式錯誤"
    End If
    Close
    Close
    End
    End Sub

    還有如果需要多次運算,可以用一些符號分隔比較容易除錯!

    回覆刪除