当前位置: 代码迷 >> Java相关 >> [求助]
  详细解决方案

[求助]

热度:78   发布时间:2005-06-24 15:13:00.0
[求助]
求解 110000内第8个大的质数: class xxs { public static void main(String[] args) { int a, b=1; for (int i=2; i<=110000; i++) { for (a=2; a<=i/2; a++) { if (i%a==0) break; } if (a<i/2) { b++; if(b==8) { System.out.println(i); break; } } } } } 结果显示的是15啊,不知道错在哪??
----------------解决方案--------------------------------------------------------

不好意思!偶已经能自行解决了啊,但不知结果是否是对的?
class xxs {  
   public static void main(String[] args) {  
      int a, b=0, resID=0;  
  int[] res = new int[10];  
      for (int i = 1; i &lt; 110000; i++) {  
        for (a = 2; a &lt;= (int)i/2; a++) {  
          if (i % a == 0) {  
            break;  
          }  
   
        }  
        if (a &gt; (int)i/2 ) {  
  res[resID] = i;  
  resID++;  
  resID %=8;  
        }  
      }  
  System.out.println(res[resID]);  
    }  
      
  }

请哪位帮助我检查检查,谢谢!!!
----------------解决方案--------------------------------------------------------
class xxs {  
   public static void main(String[] args) {  
      int a, b=0, resID=0;  
  int res=0;  
      for (int i = 110000; i &gt; 0; i--)
    {
   for (a = 2; a &lt;= (int)i/2; a++)
   {  
    if (i % a == 0)
    {  
      break;  
    }  
   
   }
   
   if (a &gt; (int)i/2 ) {  
   
    resID++;  
   }
   if(resID&gt;=8)
   {
    res = i;
    break;
   }
   
      }  
    System.out.println(res);
   }  
      
}你的程序运行的有点慢。我稍改了一下

[此贴子已经被作者于2005-6-24 16:42:20编辑过]



----------------解决方案--------------------------------------------------------
嗯,是的,我的那个运行好慢的,你这个就好多了啊,谢谢 !!
----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------
  相关解决方案