原文:https://www.pediy.com/kssd/pediy10/74160.html
第二题算法--拨开乌云见月明
怎么找算法位置的过程就略过了,算法会先计算用户名是否为a-y这25个小写字母,长度是否为12。然后用二重循环比较了一下大小,并统计了各字符的大小顺序,保存到数组tbNO[]中。这里会要求用户名中不能有重复的字符。再对注册码进行了转换,要求为0-3这四种数字,长度不小于53。
然后进入到注册码变换过程。我花了N长时间把这个过程用VB描述了一下:
Private Function CheckKey(UN As String, SN As String) As String Dim i As Long, j As Long, k As Long, l As Long Dim nRowCount As Long, n108 As Long, n10C As Long, n110 As Long, n114 As Long, n118 As Long, n11C As Long, n120 As Long, n124 As Long Dim tbTemp40(MAXLEN - 1) As Long Dim tbTemp100(100) As Long Dim tbRet(MAXLEN * 2) As Byte '返回字符串 Dim tbSN(53) As Long '注册码 Dim tbNO(MAXLEN - 1) As Long '用户名各字符的大小顺序 '----init table------ '这一段是我添加的,作用是对用户名和注册码进行转换。 For i = 1 To Len(UN) For j = 1 To Len(UN) If Mid$(UN, i, 1) > Mid$(UN, j, 1) Then tbNO(i - 1) = tbNO(i - 1) + 1 End If Next Next For i = 1 To Len(SN) tbSN(i - 1) = Val(Mid$(SN, i, 1)) If i > 53 Then Exit For Next '-------------------- '4022AC: i = 0 j = 0 k = 0 l = 0 '4022C8: Do Until i >= MAXLEN tbTemp40(i) = &H1E i = i + 1 Loop nRowCount = 0 n108 = 0 If tbSN(0) > 0 Then nRowCount = MAXLEN \ tbSN(0) End If n10C = 0 '40231B: Do Until n108 >= 2 If n10C > 0 Then tbTemp40(tbNO(n10C - 1)) = &H1E End If n10C = 0 '402350: Do Until n10C >= MAXLEN If n10C > 0 Then tbTemp40(tbNO(n10C - 1)) = &H1E End If If n108 = 0 Then tbTemp40(tbNO(n10C)) = &H28 n10C = n10C + 1 End If If n108 = 1 Then tbTemp40(tbNO(n10C)) = &H14 n10C = n10C + 1 End If '4023D3: k = 0 For i = 0 To tbSN(0) - 1 For j = 0 To nRowCount - 1 tbTemp100(i * 4 + j) = tbTemp40(k) k = k + 1 Next Next '402448: n110 = 0 n114 = 0 For i = 0 To nRowCount - 1 n110 = tbTemp100(tbSN(1) * 4 + i) + n110 n114 = tbTemp100(tbSN(2) * 4 + i) + n114 Next '4024BD: If n110 = n114 Then '============================================================================================== If (tbTemp100(tbSN(3) * 4 + tbSN(4))) = (tbTemp100(tbSN(5) * 4 + tbSN(6))) Then If (tbTemp100(tbSN(7) * 4 + tbSN(8))) = (tbTemp100(tbSN(9) * 4 + tbSN(10))) Then tbRet(l) = &H6C '"l" l = l + 1 Else tbRet(l) = &H6B '"k" l = l + 1 End If Else If (tbTemp100(tbSN(7) * 4 + tbSN(8))) = (tbTemp100(tbSN(9) * 4 + tbSN(10))) Then tbRet(l) = &H6A '"j" l = l + 1 Else tbRet(l) = &H69 '"i" l = l + 1 End If End If End If '4025B9: If n110 > n114 Then '============================================================================================== i = 0 n118 = 0 n11C = 0 tbTemp100(tbSN(1) * 4 + tbSN(11)) = tbTemp100(tbSN(12) * 4 + tbSN(13)) tbTemp100(tbSN(1) * 4 + tbSN(14)) = tbTemp100(tbSN(15) * 4 + tbSN(16)) tbTemp100(tbSN(1) * 4 + tbSN(17)) = tbTemp100(tbSN(18) * 4 + tbSN(19)) tbTemp100(tbSN(2) * 4 + tbSN(20)) = tbTemp100(tbSN(21) * 4 + tbSN(22)) tbTemp100(tbSN(2) * 4 + tbSN(23)) = tbTemp100(tbSN(24) * 4 + tbSN(25)) tbTemp100(tbSN(2) * 4 + tbSN(26)) = tbTemp100(tbSN(27) * 4 + tbSN(28)) '402712: For i = 0 To nRowCount - 1 n118 = tbTemp100(tbSN(1) * 4 + i) + n118 n11C = tbTemp100(tbSN(2) * 4 + i) + n11C Next If n118 > n11C Then '-------------------------------------------------------------------------- If (tbTemp100(tbSN(29) * 4 + tbSN(30))) = (tbTemp100(tbSN(31) * 4 + tbSN(32))) Then tbRet(l) = &H65 '"e" l = l + 1 Else tbRet(l) = &H61 '"a" l = l + 1 End If End If '4027D7: If n118 < n11C Then '-------------------------------------------------------------------------- If (tbTemp100(tbSN(33) * 4 + tbSN(34))) > (tbTemp100(tbSN(35) * 4 + tbSN(36))) Then tbRet(l) = &H67 '"g" l = l + 1 End If If (tbTemp100(tbSN(33) * 4 + tbSN(34))) < (tbTemp100(tbSN(35) * 4 + tbSN(36))) Then tbRet(l) = &H66 '"f" l = l + 1 End If If (tbTemp100(tbSN(33) * 4 + tbSN(34))) = (tbTemp100(tbSN(35) * 4 + tbSN(36))) Then tbRet(l) = &H68 '"h" l = l + 1 End If End If '4028DF: If n118 = n11C Then '-------------------------------------------------------------------------- k = 0 For i = 0 To tbSN(0) - 1 For j = 0 To nRowCount - 1 tbTemp100(i * 4 + j) = tbTemp40(k) k = k + 1 Next Next '402966: If (tbTemp100(tbSN(37) * 4 + tbSN(38))) > (tbTemp100(tbSN(39) * 4 + tbSN(40))) Then tbRet(l) = &H62 '"b" l = l + 1 End If If (tbTemp100(tbSN(37) * 4 + tbSN(38))) < (tbTemp100(tbSN(39) * 4 + tbSN(40))) Then tbRet(l) = &H63 '"c" l = l + 1 End If If (tbTemp100(tbSN(37) * 4 + tbSN(38))) = (tbTemp100(tbSN(39) * 4 + tbSN(40))) Then tbRet(l) = &H64 '"d" l = l + 1 End If End If End If '402A5C: If n110 < n114 Then '============================================================================================== i = 0 n120 = 0 n124 = 0 tbTemp100(tbSN(1) * 4 + tbSN(11)) = tbTemp100(tbSN(12) * 4 + tbSN(13)) tbTemp100(tbSN(1) * 4 + tbSN(14)) = tbTemp100(tbSN(15) * 4 + tbSN(16)) tbTemp100(tbSN(1) * 4 + tbSN(17)) = tbTemp100(tbSN(18) * 4 + tbSN(19)) tbTemp100(tbSN(2) * 4 + tbSN(20)) = tbTemp100(tbSN(21) * 4 + tbSN(22)) tbTemp100(tbSN(2) * 4 + tbSN(23)) = tbTemp100(tbSN(24) * 4 + tbSN(25)) tbTemp100(tbSN(2) * 4 + tbSN(26)) = tbTemp100(tbSN(27) * 4 + tbSN(28)) '402BB5: For i = 0 To nRowCount - 1 n120 = tbTemp100(tbSN(1) * 4 + i) + n120 n124 = tbTemp100(tbSN(2) * 4 + i) + n124 Next '402C0F: If n120 > n124 Then '-------------------------------------------------------------------------- If (tbTemp100(tbSN(41) * 4 + tbSN(42))) > (tbTemp100(tbSN(43) * 4 + tbSN(44))) Then tbRet(l) = &H66 '"f" l = l + 1 End If If (tbTemp100(tbSN(41) * 4 + tbSN(42))) < (tbTemp100(tbSN(43) * 4 + tbSN(44))) Then tbRet(l) = &H67 '"g" l = l + 1 End If If (tbTemp100(tbSN(41) * 4 + tbSN(42))) < (tbTemp100(tbSN(43) * 4 + tbSN(44))) Then tbRet(l) = &H68 '"h" l = l + 1 End If End If '402D17: If n120 < n124 Then '-------------------------------------------------------------------------- If (tbTemp100(tbSN(45) * 4 + tbSN(46))) = (tbTemp100(tbSN(47) * 4 + tbSN(48))) Then tbRet(l) = &H65 '"e" l = l + 1 Else tbRet(l) = &H61 '"a" l = l + 1 End If End If '402D8B: If n120 = n124 Then '-------------------------------------------------------------------------- k = 0 For i = 0 To tbSN(0) - 1 For j = 0 To nRowCount - 1 tbTemp100(i * 4 + j) = tbTemp40(k) k = k + 1 Next Next '402E12: If (tbTemp100(tbSN(49) * 4 + tbSN(50))) > (tbTemp100(tbSN(51) * 4 + tbSN(52))) Then tbRet(l) = &H63 '"c" l = l + 1 End If If (tbTemp100(tbSN(49) * 4 + tbSN(50))) < (tbTemp100(tbSN(51) * 4 + tbSN(52))) Then tbRet(l) = &H62 '"b" l = l + 1 End If If (tbTemp100(tbSN(49) * 4 + tbSN(50))) = (tbTemp100(tbSN(51) * 4 + tbSN(52))) Then tbRet(l) = &H64 '"d" l = l + 1 End If End If End If Loop n108 = n108 + 1 Loop CheckKey = StrConv(tbRet, vbUnicode) End Function
Private Function NameGen() As String Dim i As Long, j As Long, iChar As Long Dim i7 As Long, i8 As Long Dim s As String, sTemp As String Dim tbNO(MAXLEN - 1) As Long Randomize Timer '随机生成12位不重复字符 For i = 0 To MAXLEN - 1 iChar = 0 Do Until iChar > 0 iChar = Int(Rnd * 25) + 97 For j = 0 To i - 1 If iChar = Asc(Mid$(s, j + 1, 1)) Then iChar = 0 End If Next Loop s = s & Chr(iChar) Next '统计各字符的大小顺序 For i = 1 To MAXLEN For j = 1 To MAXLEN If Mid$(s, i, 1) > Mid$(s, j, 1) Then tbNO(i - 1) = tbNO(i - 1) + 1 End If Next Next '找到排第7和第8的字符位置 For i = 0 To MAXLEN - 1 If tbNO(i) = 6 Then i7 = i If tbNO(i) = 7 Then i8 = i Next If i7 + 1 <> i8 Then '当第8字符不在第7字符后时 sTemp = Mid$(s, i8 + 1, 1) '取出第8字符 s = Mid$(s, 1, i8) & Mid$(s, i8 + 2) '组合除第8字符的字符串 If i7 > i8 Then i7 = i7 - 1 '调整第7字符的位置 s = Mid$(s, 1, i7 + 1) & sTemp & Mid$(s, i7 + 2) '把第8字符插入到第7字符后 End If NameGen = s End Function