您的位置:程序门 -> vb -> 基础类



大侠!!!!!!  教教我,程序看不懂。


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


大侠!!!!!! 教教我,程序看不懂。
发表于:2007-08-10 14:40:19 楼主
[题目9]下面程序的功能是:找出所有各位数字互不相同的四位数,该数本身为素数,且各位数字均为素数.  

答:  

源程序:  

dim   a(4)   as   integer  

private   sub   command1_click()  

      dim   n   as   integer  

      for   n   =   1000   to   9999  

if   prime(n)   and   pf(n)   and   np(a)   then  

    list1.additem   n  

end   if  

next   n  

end   sub  

private   function   prime(byval   n   as   integer)   as   boolean  

      dim   i   as   integer  

      for   i   =   2   to   sqr(n)  

              if   n   mod   i   =   0   then   exit   function  

      next   i  

      prime   =   true  

end   function                              


???????????????????????????????????????
private   function   pf(byval   n   as   integer)   as   boolean  

        dim   i   as   integer  

        pf   =   true  

        do  

            i   =   i   +   1  

            a(i)   =   n   mod   10  

            if   a(i)   <   2   or   not   prime(a(i))   then   pf   =   false  

              n   =   n   \   10  

      loop   until   n   <=   0  

  end   function  
??????????????????????????????????????
  private   function   np(a()   as   integer)   as   boolean  

        dim   i   as   integer,   j   as   integer  

            np   =   true  

        for   i   =   1   to   ubound(a)   -   1  

                for   j   =   i   +   1   to   ubound(a)  

                      if   a(i)   =   a(j)   then   np   =   false  

                next   j  

        next   i  

end   function  

能告诉我下面代码的目的??????????和含义??????
  dim   i   as   integer  

        pf   =   true  

        do  

            i   =   i   +   1  

            a(i)   =   n   mod   10  

            if   a(i)   <   2   or   not   prime(a(i))   then   pf   =   false  

              n   =   n   \   10  

      loop   until   n   <=   0


发表于:2007-08-22 15:23:291楼 得分:0
im   i   as   integer

pf   =   true

do

i   =   i   +   1

a(i)   =   n   mod   10

if   a(i)   <   2   or   not   prime(a(i))   then   pf   =   false

n   =   n   \   10

loop   until   n   <=   0


进行素数的判断。这是主要的代码。
发表于:2007-08-22 17:36:032楼 得分:0
你自己建一个工程,然后运行测试一下

设置断点
发表于:2007-08-23 15:56:223楼 得分:0
你也可以用以下代码,方法和你的代码一样!,如果有必要可以加一个进度条.
private   sub   command2_click()
doevents
unload   me
end   sub

private   sub   command1_click()
dim   p(3)   as   integer,   pan   as   boolean,   n   as   integer
dim   dd   as   boolean,   ds   as   boolean
pan   =   true
n   =   0
for   i   =   1000   to   9999
        label1.caption   =   "正在处理数字: "   +   str(i)
        for   j   =   2   to   sqr(i   -   1)           '判断是不是素数
                if   i   mod   j   =   0   then
                        pan   =   false
                end   if
        next   j
        doevents
        if   pan   =   false   then
                pan   =   true
        else
                num   =   i
                '取得各位数字
                p(0)   =   int(num   /   1000)              
                p(1)   =   int((num   -   p(0)   *   1000)   /   100)
                p(2)   =   int((num   -   int(num   /   100)   *   100)   /   10)
                p(3)   =   int(num   -   p(0)   *   1000   -   p(1)   *   100   -   p(2)   *   10)
                dd   =   differ(p)
                doevents
                ds   =   sushu(p)
                doevents
                if   dd   =   true   and   ds   =   true   then
                  list1.additem   str(i),   n
                  label1.caption   =   label1.caption   +   chr(13)   +   chr(10)   +   "符合数字: "   +   str(i)
                  n   =   n   +   1
                end   if
        end   if
next   i
end   sub
private   function   differ(p()   as   integer)   as   boolean     '判断各位数字互不相同
for   i   =   0   to   3
        for   j   =   0   to   3
                if   i   <>   j   then
                        if   p(i)   =   p(j)   then
                                differ   =   false
                                exit   function
                        end   if
                end   if
        next   j
        doevents
next   i
differ   =   true
end   function
private   function   sushu(p()   as   integer)   as   boolean   '判断各位数字是否是素数
for   i   =   0   to   3
        for   j   =   2   to   p(i)   -   1
                if   p(i)   mod   j   =   0   then
                        sushu   =   false
                        exit   function
                end   if
        next   j
        doevents
next   i
sushu   =   true
end   function



快速检索

最新资讯
热门点击