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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Ak-47 中级黑马   /  2016-3-23 23:39  /  6396 人查看  /  24 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

1黑马币
分析以下需求,并用代码实现:
        (1)打印1-100之间的所有素数及个数
        (2)每行输出5个满足条件的数,之间用空格分隔
        (3)如果一个大于1的自然数,这个数只能被1和其本身整除,这个数就叫素数。
        (4)如:2 3 5 7 11

有没有大神,搞不懂这个素数是什么意思啊,

24 个回复

倒序浏览
for循环嵌套,采用计数器思想,
回复 使用道具 举报
师兄能详细点吗?
回复 使用道具 举报
  1. class Prog2 {
  2.         public static void main(String[] args) {
  3.                 int count = 0;
  4.                 for (int i = 1; i <= 100; i++ ) {
  5.                         if (isPrime(i)) {
  6.                                 count++;
  7.                                 System.out.print(i + " ");
  8.                                 if (count % 5 == 0) {
  9.                                 System.out.println();
  10.                                 }       
  11.                         }       
  12.                 }
  13.                 System.out.println("count =" + count);
  14.         }

  15. /*
  16. 判断一个整数是不是一个素数.
  17. */
  18.         public static boolean isPrime(int a) {
  19.                 boolean flag =true ;
  20.                 if (a != 1) {
  21.                         for (int i = 2;i < a ; i++ ) {
  22.                                 if ((a % i )== 0) {
  23.                                         flag = false;
  24.                                         break;
  25.                                 }else {
  26.                                         flag = true;
  27.                                 }
  28.                         }
  29.                        
  30.                 }else { flag = false;}
  31.                 return flag;
  32.         }
  33.        
  34. }
复制代码


基础班做作业时写的 没有注释......
回复 使用道具 举报
如果一个大于1的自然数,这个数只能被1和其本身整除,这个数就叫素数  题目不就说咯
回复 使用道具 举报
大神,你怎么这么厉害啊,我还在学基础啊,表示压力三大,不知道能上就业班不啊
回复 使用道具 举报
这道题在老师发的周末作业那里看过,很多人提交了
回复 使用道具 举报
素数,就是 除本身和1外没有其他被除数,比如 7 他素数是 1 , 7  这就是素数
回复 使用道具 举报
for循环,挑出只能被1和他本身整除的数
回复 使用道具 举报
素数就是质数;只能被1和他本身整除{主要思路是:用这个数取模比它小的数字看是否为零;如果是就说明不是素数,比如{n 就是保证:n%(n-1)!=0;n%(n-2)!=0;n%(n-3)!=0;......n%(n-(n-2))!=0都为真的话就是素数,否则就不是;;;每行五个的话,可以通过计数器解决。。。}}


class Num_2 {
        public static void main(String[] args) {
                int count=0;
                int count5=0;
                int countTemp=0;
                for (int x=2;x<=100 ;x++ ) {
                       
                        int sum = x;
                        int num = x-1;
                        while (num!=1) {
                               
                                if(sum%num == 0){
                                        countTemp++;
                                }
                                num--;
                        }

                        if(countTemp== 0){
                                System.out.print(x+"\t");
                                count++;
                                count5++;
                                if(count5 == 5){
                                        System.out.println();
                                        count5=0;
                                }
                               
                                }
                        else
                        {  
                                countTemp=0;
                                }
                       
                }
                System.out.println("素数一共"+count+"个");
        }
}
回复 使用道具 举报
Ak-47 中级黑马 2016-3-26 01:36:09
11#
大神收下我的膝盖吧
回复 使用道具 举报
素数:就是能被1和它本身整除的数,采用嵌套for循环,再加一个5的整数倍标志位,足以完成此题
回复 使用道具 举报
给的黑马币太少了 给加点
回复 使用道具 举报
如果一个大于1的自然数,这个数只能被1和其本身整除,这个数就叫素数。
两个条件,第一是大于1的自然数,那就是大于整数(自然数定义为非负整数)
第二就是除了1和自身,不能被其他数整数。比如5只能除以1或者5才能整除,就是素数。比如6除了1跟6,还能整除2或者3,那么6就不是
回复 使用道具 举报
用for循环呀,遍历
回复 使用道具 举报
class BS {
        public static void main(String[] args) {
                int count = 0;
                for(int i=2;i<=100;i++){
                        for(int j=2;j<=i;j++){
                                if(i%j==0&&i!=j){
                                        break;
                                }else if(i%j==0&&i==j){
                                        System.out.print(i+"\t");
                                        count++;
                                        if (count==5) {
                                                System.out.println();
                                                count=0;
                                        }
                                       

                                }
                        }
                }
                       
                }
        }
回复 使用道具 举报
  1. public class Demo {
  2.         public static void main(String[] args) {
  3.                 //1.定义计数器
  4.                 int count = 0;
  5.                 //2.利用for循环遍历
  6.                 for(int i = 2;i<100;i++) {
  7.                         int temp = (int) Math.sqrt(i);
  8.                         int j = 2;
  9.                         for(;j<=temp;j++) {
  10.                                 if(i%j==0){
  11.                                         break;
  12.                                 }
  13.                         }
  14.                         //3.j>temp说明是素数,然后进行统计打印
  15.                         if(j>temp) {
  16.                                 count++;
  17.                                 System.out.print(i+" ");
  18.                                 if(count%5==0) {
  19.                                         System.out.println();
  20.                                 }
  21.                         }
  22.                 }
  23.                 System.out.println();
  24.                 System.out.println("1-100的素数个数为:"+count);
  25.         }
  26. }
复制代码


以上代码,试着看看吧
回复 使用道具 举报
Ak-47 中级黑马 2016-3-28 01:02:22
18#
楼上的都是大神啊
回复 使用道具 举报
菊花爆满山 来自手机 中级黑马 2016-3-28 12:55:21
19#
素数就是质数,只能被1和自身整除的数,例如3、5、7、11、13等
回复 使用道具 举报
本帖最后由 犹豫的烤肉拌饭 于 2016-3-29 00:04 编辑
  1. class Demo {
  2.         public static void main(String[] args) {
  3.                 int count = 0;
  4.                 for (int i = 1; i < 100; i++) {
  5.                         if (suShu(i)) {
  6.                                 count++;
  7.                                 System.out.print(i + " ");
  8.                                 if (count % 5 == 0) {
  9.                                         System.out.println("");
  10.                                 }
  11.                         }

  12.                 }
  13.         }

  14.         public static boolean suShu(int i) {
  15.                 if (i == 1) {
  16.                         return true;
  17.                 }
  18.                 int count = 0;
  19.                 for (int j = 2; j <= i; j++) {
  20.                         if (i % j == 0) {
  21.                                 count++;
  22.                         }
  23.                         if (count >= 2) {
  24.                                 return false;
  25.                         }
  26.                 }
  27.                 if (count == 1) {
  28.                         return true;
  29.                 }
  30.                 return false;

  31.         }
  32. }
复制代码


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