当前位置:编程学习 > VB >>

vb编程 问题 求1到100 之间的质数 要求在写入到一个txt文本文件

输出的格式如下 少于10的质数 在前面添加零 而且txt文本文件 每行输出五个质数
,输出样本如下
02 03 05 07 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97
追问:大哥能加你qq不?以后问题都请你回答了,真的是太牛逼了
答案:Private Sub Form_Load()

  Dim i As Integer, j As Integer

  Dim p As String, tmp As String

  p = App.Path & "\a.txt"

  tmp = ""

  j = 0

  Open p For Output As #1

    For i = 2 To 100

        If IsPerime(i) Then

            j = j + 1

           tmp = tmp & " " & IIf(i < 9, "0" & i, i)

            If j = 5 Then

                Print #1, tmp

                j = 0

                tmp = ""

            End If

        End If

    Next

  Close #1

End Sub

 

Private Function IsPerime(Byval number As Integer) As Boolean

  Dim i As Integer, t As Integer

  IsPerime = True

  t = Int(Sqr(number))

  For i = 2 To t

    If number Mod i = 0 Then

        IsPerime = False

        Exit For

    End If

  Next

End Sub

Option Explicit
Const N = 100
Private Sub Form_Click()
    Dim i, j, c, s() As String
    For i = 2 To N
        For j = 2 To Int(i / 2)
            If i Mod j = 0 Then Exit For
        Next
        If j > Int(i / 2) Then
            c = c + 1
            ReDim Preserve s(1 To c)
            s(c) = IIf(c Mod 5 = 0, CStr(i) & vbNewLine, CStr(i))
            If i < 10 Then
                s(c) = "0" & s(c)
                If c = 1 Then s(c) = " " & s(c)
            End If
        End If
    Next
        Print Join(s, " ")
        Open "c:\outputfile.txt" For Output As #1
        Print #1, Join(s, " ")
        Close #1
End Sub

上一个:我想知道这两题怎么用VB编程,能帮忙吗?
下一个:关于VB的编程

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,