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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

1、public static void main(String[] args) {
                int sum=0;
                for(int x=1;x<100;x++){
                        if(x==2){
                                System.out.print(x+" ");
                                sum+=x;       
                        }
                       
                        for (int i = 2; i <x; i++) {
                                if (x%i==0) {
                                        break;
                                }
                                if(i==x-1){
                                        System.out.print(x+" ");
                                       
                                        sum+=x;
                                }
                        }
                }
               
                System.out.println("\n"+sum);
        }

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
1060

2、public static void yangHui(int num){
                int[][] a = new int[num][num];
                for (int i = 0; i < num; i++){
                        for (int j = 0; j < num; j++) {
                                if (j < i) {
                                        a[i][j] = 1;
                                        if (j == 0) {
                                                a[i][j] = 1;
                                        } else {
                                                a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
                                        }
                                } else {
                                        a[i][j] = 1;
                                }
                        }
                }
                for (int i = num-1; i >= 0; i--) {
                        for (int k = num - i; k >= 1; k--)
                                System.out.printf(" ");

                        for (int j = 0; j <= i; j++) {
                                System.out.printf("%3d ", a[i][j]);
                        }
                        System.out.printf("\n");
                }       
        }

        /**
         *
         */
        public static void main(String[] args) {
                System.out.println("请输入一个杨辉三角的行数:");
                Scanner input=new Scanner(System.in);
                int num=input.nextInt();
                yangHui(num);
        }
请输入一个杨辉三角的行数:
4
   1   3   3   1
    1   2   1
     1   1
      1
3、public static void main(String[] args) {
                int[] arr={2,5,9,10,48,95,154,31,59,69};
                for (int x = 0; x < arr.length; x++) {
                        if(arr[x]==2)
                                arr[x]=0;
                        for (int i = 2; i <arr[x]; i++) {
                                if (arr[x]%i==0){
                                       
                                     //b=false;
                                        break;
                                }
                                if(i==arr[x]-1){
                                        arr[x]=0;
                                }
                        }
                }
                String string="";
                for (int i = 0; i < arr.length; i++) {
                        System.out.print(string);
                        System.out.println(arr[i]);
                        string+="  ";
                       
                }
        }
0
  0
    9
      10
        48
          95
            154
              0
                0
                  69
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马