[原]VB.NET常用的哈希算法集 差不多30种.

华芸智森 2016-05-13 08:56:37
加精

VB.NET常用的哈希算法集.其中包括了著名的暴雪的哈希,T33哈希.......全部是网上的 C/C++代码改的(VB.NET的资源真的很少).
不同的哈希算法在分布式,布降过滤器,位图MAP等等应用得比较多...

'' </summary>
Public Class MyUnchecked

#Region "UInt64"

<StructLayout(LayoutKind.Explicit)>
Public Structure UncheckedUInt64

<FieldOffset(0)>
Private longValue As UInt64
<FieldOffset(0)>
Private intValueLo As UInt32
<FieldOffset(4)>
Private intValueHi As UInt32 '//

Private Sub New(newLongValue As UInt64)
longValue = newLongValue
End Sub

Public Overloads Shared Widening Operator CType(value As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(value)
End Operator

Public Overloads Shared Widening Operator CType(value As UncheckedUInt64) As UInt64
Return value.longValue
End Operator

Public Overloads Shared Operator *(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue * y)
End Operator

Public Overloads Shared Operator /(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue / y)
End Operator

Public Overloads Shared Operator ^(x As UncheckedUInt64, y As Double) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue ^ y)
End Operator

Public Overloads Shared Operator ^(x As UncheckedUInt64, y As UInt32) As UncheckedUInt64
Return New UncheckedUInt64(MyConvert.MyPower(x.longValue, y))
End Operator

Public Overloads Shared Operator Xor(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue Xor y)
End Operator

Public Overloads Shared Operator +(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue + y)
End Operator

Public Overloads Shared Operator -(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue - y)
End Operator

Public Overloads Shared Operator <<(x As UncheckedUInt64, y As Int32) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue << y)
End Operator

Public Overloads Shared Operator >>(x As UncheckedUInt64, y As Int32) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue >> y)
End Operator

Public Overloads Shared Operator And(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue And y)
End Operator

Public Overloads Shared Operator =(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue)
End Operator

Public Overloads Shared Operator <>(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue <> y)
End Operator

Public Overloads Shared Operator Not(x As UncheckedUInt64) As UncheckedUInt64
Return New UncheckedUInt64(Not x.longValue)
End Operator

End Structure

#End Region
End Class
...全文
6626 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
wasish 2018-04-09
  • 打赏
  • 举报
回复
多谢楼主的分享..
dyren99 2018-04-08
  • 打赏
  • 举报
回复
不错,不错,学习了。楼主威武了。
水哥阿乐 2017-05-26
  • 打赏
  • 举报
回复
不是月神推一把,这等好贴我还没看到,月神真是一个合格的版主
水哥阿乐 2017-05-26
  • 打赏
  • 举报
回复
论坛正需要你这样的人,装B的少一点,共享的实际代码多一点.你这指头轻点一下不要紧,从此中国网民百度有了哈希算法的完美资料,楼主是真的帅了
SimilarDuckweed 2017-05-26
  • 打赏
  • 举报
回复
感谢楼主无私分享。
x287634334 2017-03-16
  • 打赏
  • 举报
回复
不感谢楼主行吗?不行!必须得感谢楼主无私分享。
blandlife 2017-02-06
  • 打赏
  • 举报
回复
不错的分享!!!
叫我小才吧 2017-02-03
  • 打赏
  • 举报
回复
多谢楼主的分享..
Winters_lee 2017-02-03
  • 打赏
  • 举报
回复
标注收藏,谢谢楼主
lkj2016 2017-01-10
  • 打赏
  • 举报
回复
学习了
上海-公子 2016-12-13
  • 打赏
  • 举报
回复
楼主牛B且心胸宽广,赞一个!
cnavy_xu 2016-12-13
  • 打赏
  • 举报
回复
是俗话说的很多的
leayh 2016-12-05
  • 打赏
  • 举报
回复
楼主牛B且心胸宽广,赞一个!
正怒月神 2016-12-05
  • 打赏
  • 举报
回复
threenewbee 2016-12-04
  • 打赏
  • 举报
回复
感谢lz的原创和分享,给推荐下
zenter 2016-12-03
  • 打赏
  • 举报
回复
楼主好人,好人发大财,一生平安
华芸智森 2016-05-13
  • 打赏
  • 举报
回复

    Public Shared Function HashSDBM(Key As String) As UInt64

        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        For i As Int32 = 0 To L - 1
            hash = Asc(KeyCharArr(i)) + (hash << 6) + (hash << 16) - hash
        Next

        Return (hash And &H7FFFFFFF)

    End Function

    Public Shared Function HashSDBM(KeyByte() As Byte) As UInt64

        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = KeyByte.Length - 1

        For i As Int32 = 0 To L - 1
            hash = KeyByte(i) + (hash << 6) + (hash << 16) - hash
        Next

        Return (hash And &H7FFFFFFF)

    End Function


    Public Shared Function HashJS(Key As String) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 1315423911
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        For i As Int32 = 0 To L - 1
            hash = hash Xor (((hash << 5) + Asc(KeyCharArr(i)) + (hash >> 2)))
        Next
        Return hash
    End Function

    Public Shared Function HashJS(KeyByte() As Byte) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 1315423911
        Dim L As Int32 = KeyByte.Length - 1

        For i As Int32 = 0 To L - 1
            hash = hash Xor (((hash << 5) + KeyByte(i) + (hash >> 2)))
        Next
        Return hash
    End Function

    Public Shared Function HashPJW(Key As String) As UInt64

        Dim BitsInUnsignedInt As UInt64 = CLng(4 * 8)
        Dim ThreeQuarters As UInt64 = CLng((BitsInUnsignedInt * 3) / 4)
        Dim OneEighth As UInt64 = CLng(BitsInUnsignedInt / 8)
        Dim HighBits As UInt64 = CLng(&HFFFFFFFF) << (BitsInUnsignedInt - OneEighth)
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim test As UInt64 = 0
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray

        For I As Int32 = 0 To L
            hash = (hash << OneEighth) + Asc(KeyCharArr(I))
            If ((test = hash And HighBits) <> 0) Then
                hash = ((hash Xor (test >> ThreeQuarters)) And (Not HighBits))
            End If

        Next

        Return hash

    End Function

    Public Shared Function HashPJW(KeyByte() As Byte) As UInt64

        Dim BitsInUnsignedInt As UInt64 = CLng(4 * 8)
        Dim ThreeQuarters As UInt64 = CLng((BitsInUnsignedInt * 3) / 4)
        Dim OneEighth As UInt64 = CLng(BitsInUnsignedInt / 8)
        Dim HighBits As UInt64 = CLng(&HFFFFFFFF) << (BitsInUnsignedInt - OneEighth)
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim test As UInt64 = 0
        Dim L As Int32 = KeyByte.Length - 1

        For I As Int32 = 0 To L
            hash = (hash << OneEighth) + KeyByte(I)
            If ((test = hash And HighBits) <> 0) Then
                hash = ((hash Xor (test >> ThreeQuarters)) And (Not HighBits))
            End If

        Next

        Return hash

    End Function

    Public Shared Function HashAP(Key As String) As UInt64

        Dim hash As MyUnchecked.UncheckedUInt64 = &HAAAAAAAA
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        For i As Int32 = 0 To L
            If ((i And 1) = 0) Then
                hash = hash Xor (((hash << 7) Xor Asc(KeyCharArr(i)) * (hash >> 3)))
            Else
                hash = hash Xor ((Not ((hash << 11) + Asc(KeyCharArr(i)) Xor (hash >> 5))))
            End If
        Next

        Return hash

    End Function

    Public Shared Function HashAP(KeyByte() As Byte) As UInt64

        Dim hash As MyUnchecked.UncheckedUInt64 = &HAAAAAAAA
        Dim L As Int32 = KeyByte.Length - 1

        For i As Int32 = 0 To L
            If ((i And 1) = 0) Then
                hash = hash Xor (((hash << 7) Xor KeyByte(i) * (hash >> 3)))
            Else
                hash = hash Xor ((Not ((hash << 11) + KeyByte(i) Xor (hash >> 5))))
            End If
        Next
        Return hash

    End Function

    Public Shared Function HashDEK(Key As String) As UInt64

        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        Dim hash As MyUnchecked.UncheckedUInt64 = L + 1
        For i As Int32 = 0 To L
            hash = ((hash << 5) Xor (hash >> 27)) Xor Asc(KeyCharArr(i))
        Next
        Return hash

    End Function


    Public Shared Function HashDEK(KeyByte() As Byte) As UInt64

        Dim L As Int32 = KeyByte.Length - 1
        Dim hash As MyUnchecked.UncheckedUInt64 = L + 1
        For i As Int32 = 0 To L
            hash = ((hash << 5) Xor (hash >> 27)) Xor KeyByte(i)
        Next
        Return hash

    End Function

    Public Shared Function HashELF(Key As String) As UInt64

        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim x As Long = 0
        For i As Int32 = 0 To L
            hash = (hash << 4) + Asc(KeyCharArr(i))
            x = hash And &HF0000000L
            If x <> 0 Then
                hash = hash Xor (x >> 24)
            End If
            hash = hash And (Not x)
        Next
        Return hash

    End Function

    Public Shared Function HashELF(KeyByte() As Byte) As UInt64

        Dim L As Int32 = KeyByte.Length - 1
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim x As Long = 0
        For i As Int32 = 0 To L
            hash = (hash << 4) + KeyByte(i)
            x = hash And &HF0000000L
            If x <> 0 Then
                hash = hash Xor (x >> 24)
            End If
            hash = hash And (Not x)
        Next

        Return hash

    End Function


    Private Shared cryptTable(&H100 * 5 - 1) As UInt64
    Private Shared IsInitcryptTable As Boolean = False
    Public Shared Sub HashBlizzardInit()

        Dim seed As UInt64 = &H100001
        Dim index1 As UInt64 = 0
        Dim index2 As UInt64 = 0
        Dim I As UInt64
        Dim KKK As UInt64 = 0
        For index1 = 0 To &H100 - 1
            index2 = index1
            For I = 0 To 4
                Dim temp1, temp2 As UInt64
                seed = (seed * 125 + 3) Mod &H2AAAAB
                temp1 = (seed And &HFFFF) << &H10
                seed = (seed * 125 + 3) Mod &H2AAAAB
                temp2 = (seed And &HFFFF)
                cryptTable(index2) = (temp1 Or temp2) '//|
                index2 += &H100
            Next
        Next

        IsInitcryptTable = True

    End Sub

    ''' <summary>
    ''' 暴雪公司出名的哈希码.
    ''' 测试了 二千万 GUID, 没有重复.但运算量比较大。
    ''' </summary>
    ''' <param name="key"></param>
    ''' <param name="HasType">HasType =0 ,1 ,2 </param>
    ''' <returns></returns>
    Public Shared Function HashBlizzard(ByVal Key As String,
                                        Optional HasType As Long = 0) As UInt64

        If IsInitcryptTable = False Then HashBlizzardInit()

        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        Dim seed1 As MyUnchecked.UncheckedUInt64 = &H7FED7FED
        Dim seed2 As MyUnchecked.UncheckedUInt64 = &HEEEEEEEE
        Dim LoopID As Int32 = 0
        While (LoopID < L)
            Dim ascCode As Int32 = Asc(KeyCharArr(LoopID))
            seed1 = cryptTable((HasType << 8) + ascCode) Xor (seed1 + seed2)
            seed2 = ascCode + seed1 + seed2 + (seed2 << 5) + 3
            LoopID += 1
        End While

        Return seed1

    End Function

    ''' <summary>
    ''' 暴雪公司著名的 HashMap .
    ''' 测试了 二千万 GUID, 没有重复.但运算量比较大。
    ''' </summary>
    ''' <param name="KeyByte"></param>
    ''' <param name="HasType">HasType =[0 ,1 ,2] </param>
    ''' <returns></returns>
    Public Shared Function HashBlizzard(KeyByte() As Byte,
                                        Optional HasType As Long = 0) As UInt64

        If IsInitcryptTable = False Then HashBlizzardInit()

        Dim L As Int32 = KeyByte.Length - 1
        Dim seed1 As MyUnchecked.UncheckedUInt64 = &H7FED7FED
        Dim seed2 As MyUnchecked.UncheckedUInt64 = &HEEEEEEEE
        Dim LoopID As Int32 = 0
        While (LoopID < L)
            Dim ascCode As Int32 = KeyByte(LoopID)
            seed1 = cryptTable((HasType << 8) + ascCode) Xor (seed1 + seed2)
            seed2 = ascCode + seed1 + seed2 + (seed2 << 5) + 3
            LoopID += 1
        End While

        Return seed1

    End Function
华芸智森 2016-05-13
  • 打赏
  • 举报
回复

    ''' <summary>
    ''' 经典times33算法。简单高效。[这个使用移位代替*33]
    ''' 测试一千万。没有重复哈希值。
    ''' </summary>
    ''' <param name="Key"></param>
    ''' <returns></returns>
    Public Shared Function HashCMyMap(Key As String) As UInt64

        Dim nHash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        Dim I As Int32 = 0

        While (I < L)
            nHash = (nHash << 5) + nHash + Asc(KeyCharArr(I)) + 3
            I += 1
        End While

        Return nHash

    End Function


    ''' <summary>
    ''' 经典times33算法。简单高效。[这个使用移位代替*33]
    ''' 测试一千万。没有重复哈希值。
    ''' </summary>
    ''' <param name="KeyByte"></param>
    ''' <returns></returns>
    Public Shared Function HashCMyMap(KeyByte() As Byte) As UInt64

        Dim nHash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = KeyByte.Length - 1
        Dim I As Int32 = 0

        While (I < L)
            nHash = (nHash << 5) + nHash + KeyByte(I) + 3
            I += 1
        End While

        Return nHash

    End Function


    ''' <summary>
    ''' 经典的Time算法。简单,高效。
    ''' Ngix使用的是 time31,Tokyo Cabinet使用的是 time37
    ''' 小写英文词汇适合33, 大小写混合使用65。time33比较适合的是英文词汇的hash.
    ''' </summary>
    ''' <param name="Key"></param>
    ''' <param name="seed">种子数。 31,33,37 。。。</param>
    ''' <returns></returns>
    Public Shared Function HashTimeMap(Key As String,
                                       seed As Int16) As UInt64

        Dim nHash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        Dim I As Int32 = 0

        While (I < L)
            nHash = seed * nHash + nHash + Asc(KeyCharArr(I)) + 3
            I += 1
        End While

        Return nHash

    End Function

    ''' <summary>
    ''' 经典的Time算法。简单,高效。
    ''' Ngix使用的是 time31,Tokyo Cabinet使用的是 time37
    ''' 小写英文词汇适合33, 大小写混合使用65。time33比较适合的是英文词汇的hash.
    ''' </summary>
    ''' <param name="KeyByte"></param>
    ''' <param name="seed">种子质数。 31,33,37 。。。</param>
    ''' <returns></returns>
    Public Shared Function HashTimeMap(KeyByte() As Byte,
                                       seed As UInt32) As UInt64

        Dim nHash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = KeyByte.Length - 1
        Dim I As Int32 = 0

        While (I < L)
            nHash = seed * nHash + nHash + KeyByte(I) + 3
            I += 1
        End While

        Return nHash

    End Function


#End Region
华芸智森 2016-05-13
  • 打赏
  • 举报
回复

#Region "哈希算法"


    ''' <summary>
    ''' 和 HashCMyMap 基本一样.
    ''' </summary>
    ''' <param name="Key"></param>
    ''' <returns></returns>
    Public Shared Function HashDJB(Key As String) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 5381
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray

        For i As Int32 = 0 To L
            hash = ((hash << 5) + hash) + Asc(KeyCharArr(i)) + 3
        Next

        Return hash

    End Function

    ''' <summary>
    ''' 和 HashCMyMap 基本一样.
    ''' </summary>
    ''' <param name="KeyByte"></param>
    ''' <returns></returns>
    Public Shared Function HashDJB(ByVal KeyByte() As Byte) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 5381
        Dim L As Int32 = KeyByte.Length - 1

        For i As Int32 = 0 To L
            hash = ((hash << 5) + hash) + KeyByte(i) + 3
        Next

        Return hash

    End Function

    ''' <summary>
    ''' BKDR 哈希
    ''' </summary>
    ''' <param name="Key"></param>
    ''' <param name="seed">种子.最好是使用质数.</param>
    ''' <returns></returns>
    Public Shared Function HashBKDR(ByVal Key As String,
                                    Optional seed As Long = 131) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        For i As Int32 = 0 To L - 1
            hash = (hash * seed) + Asc(KeyCharArr(i)) + 3
        Next

        Return (hash And &H7FFFFFFF)

    End Function

    ''' <summary>
    ''' BKDR 哈希
    ''' </summary>
    ''' <param name="KeyByte"></param>
    ''' <param name="seed">种子数</param>
    ''' <returns></returns>
    Public Shared Function HashBKDR(ByVal KeyByte() As Byte,
                                    Optional seed As Long = 131) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = KeyByte.Length - 1
        For i As Int32 = 0 To L - 1
            hash = (hash * seed) + KeyByte(i) + 3
        Next

        Return (hash And &H7FFFFFFF)

    End Function

    Public Shared Function HashRS(Key As String,
                                  Optional seed As Long = 131) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim b As UInt64 = 378551
        Dim a As UInt64 = 63689
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        For i As Int32 = 0 To L - 1
            hash = (hash * a) + Asc(KeyCharArr(i))
            a = a * b
        Next

        Return (hash And &H7FFFFFFF)

    End Function

    Public Shared Function HashRS(KeyByte() As Byte,
                                  Optional seed As Long = 131) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim b As UInt64 = 378551
        Dim a As UInt64 = 63689
        Dim L As Int32 = KeyByte.Length - 1

        For i As Int32 = 0 To L - 1
            hash = (hash * a) + KeyByte(i)
            a = a * b
        Next

        Return (hash And &H7FFFFFFF)

    End Function
水仙花数的vfp实现 时间:2009-05-08来源:编程入门网 作者:老马   本文作者“老马”为编程入门网VFP专栏作家,转载请保留这句话。   记得看过朋友的文章中有句话大致是这个意思:一个人编程的水平与实现同一目的所需的代码数量成反比。其实这句话很有道理,如果用这个观点来评价我自己,我应该是一个苍老的菜鸟,我做出来的东西勉强可用,但代码在高手看来却是惨不忍睹。我所走过的轨迹与常人大概也有所不同:计算机专业科班的可能在毕业后从事coding多年之后,当感觉自己力不从心时转行做管理或教师;而我恰恰相反,外贸专业毕业后狂热地自学了一些东西,教了几年C语言、vb、vfp及asp等课程,几乎所有的业余时间都用到了接活来做上,这一点也差不多达到了狂热的程度。不过后来发现自己似乎是走进了死胡同:我试图走进VC的世界,可是被MFC、SDK这些东西折磨得痛苦之极;当我为自己开始能用asp做些东西而沾沾自喜的时候,一个强调代码与界面分离的asp.net一天比一天时髦起来,而它的身后是一个.NET家族。更新的技术何时出现?天知道,或许就在明天。我终于开始明白,我实际并不象身边的人说的那样聪明,我也只是一个平庸的人;我终于开始明白,如果在我所从事的这个没有任何保障的“挨踢”行业继续做下去,过几年我会成为40、50人员,那时恐怕连愿意为我交社保、医保的地方都找不到了。所以现在我进入了企业,不再是教师,因为这需要很大的精力来证明自己的能力和水平;也不再为了些“水票管理”之类的东西而通宵达旦,因为这样做得到的是几个小钱,失去的却是最宝贵的健康。   今天和几个高中同学小聚了一下,或许是人岁数大了愿意回忆从前的事,或许是还有些酒意,所以有了上面的这一段话。不过我想这不应该算是牢骚,应该说是一个菜鸟对自己的可笑经历的总结。   我始终认为C语言是一个基础性的语言,以前无论是VB或VFP课,我总会要求同学自己找本谭浩强的《C程序设计》,把预处理命令、指针、结构体与共用体等几部分内容pass过去,阅读其基础部分的内容;而在课时充足的情况下,我也会经常拿C程序书中诸如“鸡兔同笼”这样有趣的题来“折磨”一下班上的同学。   而现在有些日子了,有两个高中同学总跑到我家里来。人活到老、学到老,这本是件好事,不过我还是有些苦恼。因为他们到我家里来学习,不仅不交学费,我还得管他们饭。他们问到的vfp的东西比较多,所以我打算继续把这方面的一些东西翻出来晒晒。从本文开始会探讨一下C程序书中比较经典的几个算法在VFP中实现的问题,当然纯属菜鸟之见,有没有用那可两说,得自己去分析。觉得有用的话,或许可以开拓思路、扩充您的知识面,我很高兴;觉得没用的话,您权当我是吃撑了,您能忍受我罗嗦了这么长时间,我一样很高兴,呵呵。   水仙花数的实现是一个比较经典的算法题,今天我们首先在vfp中来实现它。   首先我们了解一下什么是“水仙花数”。所谓水仙花数是指一个n位数,其各位数字立方和等于该数本身的值,例如:153=13+53+33 ,所以153是一个水仙花数。   我们来做一个简单点儿的:求解3位数的水仙花数,即100至999之间的水仙花数。很明显这个程序需要使用循环,并且从水仙花数的概念可知,其重点是求解出循环变量当前值的各位数字的值。剩下的工作就简单了,把求解出的各位数字的立方和与循环变量当前值进行比较,如果相等则说明这是一个水仙花数,输出它即可。相关说明见代码注释。本文发表于编程入门网:www.bianceng.cn   我们用表单来实现这个例子,运行时如下图:   参照上图开始我们的制作:   一、新建表单,向表单上添加一个标签控件,caption属性值设置为“显示100到999间的水仙花数”;添加两个命令按钮command1和command2,并将它们的caption属性值分别设置为“开始”和“清除”;添加一个编辑框控件Edit1,属性值均采用默认的。   二、添加事件代码:   1、“清除”按钮的click事件: thisform.edit1.value="" thisform.refresh   2、“开始”按钮的click事件: local i,a,b,c for i=100 to 999 a=int(i/100) &&百位的值等于这个三位数除以100后取整 b=int((i-100*a)/10) &&用这个三位数减去它的百位数字与100乘积, &&对得到的差除以10后进行取整,结果就是十位的值 c=i-int(i/10)*10 &&与上面理相同,这是求个位的值 if i=a^3+b^3+c^3 &&成立说明是水仙花数并输出 thisform.edit1.value=thisform.edit1.value+str(i,5)+chr(13) &&用chr(13)换行 endif endfor

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧