Nanosoft資訊軟體公司研發出一套商用軟體,準備於市面上販售,但唯恐遭到未經授權之不合法使用,因此在安裝軟體時,需要使用者輸入授權馬。試寫一個程式,其功能可以檢核此授權碼的正確信。
【規則敘述】
此授權碼由十個位數合成,每一位數可以為0~9的任何一個數字,是判斷方法如下:
首先,將此碼逐次的累加,使的第二位數成為第一位數到第二位數的和,第三位數為第一位數到第三位數的和....第十位數為第一位數到第十位數的和;
進行完第一次的累加和後 ,接著再將所得到的十個數字,第十個數字減掉第一個數字,第九個數字減掉第二個數字....以此類推,得到五組數字,將此五組數字由小到大排列後成為一個新的數列。
此新數列的後三碼為識別碼,必須可為11整除,方為正確的授權碼,故在螢幕上列印出"此為合法之授權碼",反之則印出"此為不合法之授權碼",若授權碼格式錯誤則顯示"授權碼格式錯誤"。
輸入範例:
請輸入授權碼:9476282354
輸出範例:
此為合法之授權碼
【提示】
輸入授權碼為9476282354
第一次運算後得到數列 9 13 20 26 28 36 38 41 46 50
第二次運算後得到數列 41 33 21 12 8
排序後得到數列引 812213341 (341被11整除)
參照: http://chscvb.blogspot.com/2010/02/20100201.html
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 a(10), z(10) As Integer
Input #1, x
b = Len(x)
For i = 1 To b
a(i) = Mid(x, i, 1)
Next
For y = 1 To 10
For i = 1 To y
z(y) = z(y) + a(i)
Next
Next
i = 10
For y = 1 To 5
z(y) = z(i) - z(y)
i = i - 1
Next
For i = 5 To 1 Step -1
ans = ans & z(i)
Next
ans = Right(ans, 3)
ans = ans Mod 11
If ans = 0 Then Print #2, "此為合法之授權碼" Else Print #2, "此為不合法之授權碼"
Close
Close
End
End Sub
小冰好,
回覆刪除程式正確。
但是,這題寫程式時,有時候會遇到奇怪的問題哦。
字串,數字,兩個的處理方式,有時會怪怪的。
程式在寫的時候,沒有定義變數是字串還是數字。
在用函數時,在用加法時,又分別當它是字串和數字。
來去之間,有時會出現找不到錯誤點的錯誤哦。
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 num(10) As Integer
Input #1, n
If Len(n) <> 10 Then GoTo A:
For i = 1 To 10
num(i) = Mid(n, i, 1)
Next i
For i = 2 To 10
num(i) = num(i) + num(i - 1)
Next i
c = 10
For i = 1 To 5
num(i) = num(c) - num(i)
c = c - 1
Next i
For i = 1 To 5
A = num(i) & A
Next i
ans = Right(A, 3)
If ans Mod 11 = 0 Then
Print #2, "此為合法之授權碼"
Else
A: Print #2, "此為不合法之授權碼"
End If
Close #2
Close #1
End
End Sub
晟晟、小冰被我抓到你們沒做這個
回覆刪除「若授權碼格式錯誤則顯示"授權碼格式錯誤"。」
雖然只是一個IF啦-/-
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 n(10), m(10) As Integer
Dim s(5) As Integer
Input #1, x
If Len(x) = 10 Then
For i = 1 To 10
n(i) = Mid(x, i, 1)
Next i
For j = 1 To 10
For k = 1 To j
m(j) = m(j) + n(k)
Next k
Next j
a = 10
For b = 1 To 5
m(b) = m(a) - m(b)
a = a - 1
Next b
For c = 5 To 1 Step -1
ans = ans & m(c)
Next c
If Val(Right(ans, 3)) Mod 11 = 0 Then
Print #2, "此為合法之授權碼"
Else
Print #2, "此為不合法之授權碼"
End If
Else
Print #2, "授權碼格式錯誤"
End If
Close #2
Close #1
End
End Sub
Private Sub Form_Load()
回覆刪除Dim b(1 To 10), c(4), SumA As Integer
Dim strA As String
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, n
For i = 1 To 10
b(i) = Mid(n, i, 1)
Next i
SumA = 0
For i = 1 To 10
SumA = SumA + Val(b(i))
b(i) = SumA
Next i
d = 1: e = 10
For i = 0 To 4
c(i) = b(e) - b(d)
d = d + 1
e = e - 1
Next i
For i = 0 To 4
For j = 0 To 3
If Val(c(j)) <= Val(c(j + 1)) Then
g = c(j)
c(j) = c(j + 1)
c(j + 1) = g
End If
Next j
Next i
For i = 0 To 4
strA = strA & c(i)
Next i
If Val(strA) Mod 11 = 0 Then
Print #2, "此為合法之授權碼"
Else
Print #2, "此為不合法之授權碼"
End If
Close #1
Close #2
End
End Sub