A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xinchenglong 中级黑马   /  2013-8-26 14:21  /  2104 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

昨天的时候,我做测试题,碰到一个判断是否为素数的题目,想了很长时间,但是不怎么好理解,求各位黑马人员各抒己见,有没有好的,更优的方式来解决这个问题呢?(遍历)

9 个回复

倒序浏览
  1.         void getSushu(){
  2.                
  3.                 for(int i=101;i<200;i++){
  4.            boolean b=false;
  5.                        
  6.                   double Num=0;
  7.                   if(i%2!=0){
  8.                           Num=Math.sqrt(i);
  9.                           for(int j=2;j<=Num;j++){
  10.                                   if(i%j==0){
  11.                                           b=false;
  12.                                           break;
  13.                                   }else{
  14.                                
  15.                                           b=true;
  16.                                                   }
  17.                                   }
  18.                           if(b==true){
  19.                                   System.out.println(i);
  20.                                   
  21.                                 }
  22.                           }
  23.                 }
复制代码
这是我做的一个1求从101到200之间的素数,求任意的素数只需要改变循环下表,你可以看下,希望对你有用

评分

参与人数 1技术分 +1 收起 理由
赵宗荣 + 1

查看全部评分

回复 使用道具 举报

这两个网页你可以看看,希望能帮到你
http://www.cnblogs.com/CSharpSPF/archive/2012/04/04/2432105.html
http://www.cnblogs.com/luluping/archive/2010/03/03/1677552.html
回复 使用道具 举报
把我这笨拙的代码发来看看:
  1. using System;
  2. namespace Que
  3. {
  4.         class Program
  5.         {
  6.                 static void Main(string[] args)
  7.                 {
  8.                         string sInput = "";
  9.                         while(true)
  10.                         {
  11.                                 Console.WriteLine("请输入一个数,系统将判断此数是否为素数,若输入为quit,则退出程序");
  12.                                 sInput = Console.ReadLine();
  13.                                 if(sInput == "quit")
  14.                                 {
  15.                                         Console.WriteLine("程序即将退出...");
  16.                                         break;
  17.                                 }
  18.                                 //能运行到这里,说明用户没有输入"quit",接下来判断是否是合法的
  19.                                 int iNum;
  20.                                 bool bifSuccess = int.TryParse(sInput,out iNum);
  21.                                 if(!bifSuccess)
  22.                                 {
  23.                                         Console.WriteLine("输入有误,程序将重新执行...");
  24.                                         continue;
  25.                                 }
  26.                                 //能运行到这里说明,可以判断这个数是否为素数了
  27.                                 //iNum是这个数,从iNum到1判断,iNum包含不包含除了1、它本身之外的约数
  28.                                 //1 1%[1,1]  2 2%[1,2] ...
  29.                                 bool biNumisSushu = true;                               
  30.                                 if(iNum != 1&&iNum != 2)
  31.                                 {
  32.                                         for(int i = iNum-1;i>=2;i--)
  33.                                         {
  34.                                                 if(iNum%i==0)
  35.                                                 {       
  36.                                                         if(i!=1&&i!=iNum)
  37.                                                         {
  38.                                                                 Console.WriteLine("这个数不是素数!");
  39.                                                                 biNumisSushu = false;
  40.                                                                 break;
  41.                                                         }
  42.                                                 }                                       
  43.                                         }
  44.                                 }                               
  45.                                 //运行到这里,说明这个数是素数
  46.                                 if(biNumisSushu)
  47.                                 {
  48.                                         Console.WriteLine("这个数是素数!");                                       
  49.                                 }       
  50.                         }                       
  51.                 }
  52.         }
  53. }
复制代码
回复 使用道具 举报
static void Main(string[] args)
        {
        
              for(int i=2;i<=100;i++)
            {
                 if(IsNumberOK(i))
                  { //判断是素数时
                  Console.WriteLine(i);//这是判断成功的数
                   }
            }
              Console.ReadKey();
        }
         static bool IsNumberOK(int number)
                  {
                       //判断是否为素数
                        //规则是什么自己去写
                  }

评分

参与人数 1技术分 +1 收起 理由
赵宗荣 + 1

查看全部评分

回复 使用道具 举报
{:soso_e127:}你说的很有道理啊,还是没有解决根本问题啊!还是要谢谢你啊……
回复 使用道具 举报
楼上的帅哥美女们,多谢啦
回复 使用道具 举报
小天 中级黑马 2013-8-27 12:25:58
8#
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace test7
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //判断某个数是不是素数
  12.             int num = 7;
  13.             int count = 0;
  14.             for (int i = 1; i <= num; i++)
  15.             {
  16.                 if (num % i == 0)
  17.                 {
  18.                     count++;
  19.                 }
  20.             }
  21.             if (count > 2)
  22.             {
  23.                 Console.WriteLine("{0}不是素数",num);
  24.             }
  25.             else
  26.             {
  27.                 Console.WriteLine("{0}是素数",num);
  28.             }
  29.             Console.ReadKey();
  30.         }
  31.     }
  32. }
复制代码
回复 使用道具 举报
赵祥 初级黑马 2013-8-28 01:18:00
9#
最优算法,判断这个数是不是素数,只需循环到这个数的开平方根即可 ,比如求100是否素数,只需求到√100即可判断出来是否素数了
回复 使用道具 举报
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace test7
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //判断某个数是不是素数
  12.             int num = 1;//要判断的数   
  13.             bool isSuShu = true;
  14.             if (num!=1&&num!=0)//0和1既不是素数也不是合适
  15.             {
  16.                 for (int i = 2; i <= Math.Sqrt(num); i++)
  17.                 {
  18.                     if (num % i == 0)
  19.                     {
  20.                         isSuShu = false;
  21.                         break;
  22.                     }               
  23.                 }
  24.             }
  25.             else
  26.             {
  27.                 isSuShu = false;
  28.             }
  29.             if (true==isSuShu)
  30.             {
  31.                 Console.WriteLine(num+":是素数");
  32.             }
  33.             else
  34.             {
  35.                 Console.WriteLine(num + ":不是素数");
  36.             }
  37.          
  38.            
  39.            
  40.             Console.ReadKey();
  41.         }
  42.     }
  43. }
复制代码
借用楼上的代码,我做一点小小的优化

评分

参与人数 1技术分 +1 收起 理由
赵宗荣 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马