第三章习题(2)

下面是第三章后面的题:

3.11题:

分析一下:

输入一个数字
再判断数字是否为0
如果为0则退出
不是则继续
 

下面是代码:

import java.util.Scanner;
class Demo03_11{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        double sum=0;  //总和
        int positives=0;    //正数的个数
        int negatives=0;    //负数的个数
        System.out.print("请输入若干数字:");
        while(true){
            int num=scanner.nextInt();
            if(num!=0){
                sum+=num;
                if(num>0){
                    positives++;
                }else{
                    negatives++;
                }
            }else{
                break;  //跳出当前循环
            }
        }   
        if(positives+negatives==0){
            System.out.println("没有其他数字的输入,除了0");
        }else{
            System.out.println("正数的个数"+positives);
            System.out.println("负数的个数"+negatives);
            System.out.println("总和"+sum);
            System.out.println("平均值"+sum/(positives+negatives)); 
        }
    }
}

输入几个数字,看一下结果:

请输入若干数字:1 2 -1 3 0
正数的个数3
负数的个数1
总和5.0
平均值1.25

我们把第四行数字拿出来,看一下规律:
 4  3  2  1 2 3 4
-3 -2 -2  0 1 2 3
第4行 x∈[-3,3] y=|x|+1
第5行 x∈[-4,4] y=|x|+1
 

绝对值函数,图如下:

代码如下:

import java.util.Scanner;
class Demo03_14{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入行数:");
        int line=scanner.nextInt();
        for(int i=1;i<=line;i++){
            for(int k=1;k<=(line-i);k++){
                if(line<10){
                    System.out.print("  ");
                }else{
                    System.out.print("   ");
                }
            }
            for(int x=-(i-1);x<=i-1;x++){
                if(line<10){
                    System.out.printf("%-2d",Math.abs(x)+1);
                }else{
                    System.out.printf("%-3d",Math.abs(x)+1);
                }
            }
            System.out.println();
        }
    }
}

输入数字看结果:

请输入行数:7
            1
          2 1 2
        3 2 1 2 3
      4 3 2 1 2 3 4
    5 4 3 2 1 2 3 4 5
  6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
请输入行数:12
                                 1
                              2  1  2
                           3  2  1  2  3
                        4  3  2  1  2  3  4
                     5  4  3  2  1  2  3  4  5
                  6  5  4  3  2  1  2  3  4  5  6
               7  6  5  4  3  2  1  2  3  4  5  6  7
            8  7  6  5  4  3  2  1  2  3  4  5  6  7  8
         9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9
      10 9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  10
   11 10 9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  10 11
12 11 10 9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  10 11 12

class Demo03_15{
    public static void main(String[] args){
        for(int i=1;i<=6;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j+" ");
            }
            System.out.println();
        }
        System.out.println("==========");
        for(int i=1;i<=6;i++){
            for(int j=1;j<=7-i;j++){
                System.out.print(j+" ");
            }
            System.out.println();
        }
        System.out.println("==========");
        for(int i=1;i<=6;i++){
            for(int k=1;k<=6-i;k++){
                System.out.print("  ");
            }
            for(int j=i;j>=1;j--){
                System.out.print(j+" ");
            }
            System.out.println();
        }
        System.out.println("==========");
        for(int i=1;i<=6;i++){
            for(int k=1;k<=i-1;k++){
                System.out.print("  ");
            }
            for(int j=1;j<=7-i;j++){
                System.out.print(j+" ");
            }
            System.out.println();
        }
    }
}

看一下结果:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
==========
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
==========
          1
        2 1
      3 2 1
    4 3 2 1
  5 4 3 2 1
6 5 4 3 2 1
==========
1 2 3 4 5 6
  1 2 3 4 5
    1 2 3 4
      1 2 3
        1 2
          1

3.16题:

/*
第4行  
         1  2  4 8 4 2 1
         0  1  2 3 2 1 0
      x -3 -2 -1 0 1 2 3
第5行
        1 2 4 8 16 8 4 2 1
        0 1 2 3 4  3 2 1 0
    x  -4-3-2-1 0  1 2 3 4
*/
class Demo03_16{
    public static void main(String[] args){
        for(int i=1;i<=8;i++){
            for(int k=1;k<=8-i;k++){
                System.out.print("    ");
            }
            for(int x=-(i-1);x<=i-1;x++){
                System.out.printf("%4d",(int)Math.pow(2,i-1-Math.abs(x)));
            }
            System.out.println();
        }
    }
}

对这道题进行分析:

什么是素数 就是除了1和其本身之外 没有其他的数字可以整除的
num
2 ~ m ~ num-1 找到一个数字 如果这个数字m num%m==0 num不是素数
如果一个都没有找到的话 这个数字是素数

class Demo03_17{
    public static void main(String[] args){
        int count=0;  //当前素数的个数
        boolean flag=true;
        for(int num=2;num<=1000;num++){
            for(int m=2;m<=num-1;m++){
                if(num%m==0){
                    flag=false;
                    break;
                }
            }
            if(flag){
                count++;
                System.out.print(num+" ");
                if(count%8==0){ //8 16 24 32
                    System.out.println();
                }
            }
            flag=true;
        }
    }
}
/*
int num=10;
        boolean flag=true;  //默认这个数字是素数
        for(int m=2;m<=num-1;m++){
            if(num%m==0){//说明num不是素数
                flag=false;//此时不是素数
                break;
            }
        }
        //如果for正常执行完毕 都没有找到 是素数
        // System.out.println("是素数");
        //重点就在于 for是如何退出的
        //如果for是break出来的 -> 不是素数
        //如果for是正常执行完毕的->是素数
        if(flag){
            System.out.println("是素数");
        }else{
            System.out.println("不是素数");
        }
*/

看一下结果:

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 101 103 107 109 113 127 131
137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223
227 229 233 239 241 251 257 263
269 271 277 281 283 293 307 311
313 317 331 337 347 349 353 359
367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457
461 463 467 479 487 491 499 503
509 521 523 541 547 557 563 569
571 577 587 593 599 601 607 613
617 619 631 641 643 647 653 659
661 673 677 683 691 701 709 719
727 733 739 743 751 757 761 769
773 787 797 809 811 821 823 827
829 839 853 857 859 863 877 881
883 887 907 911 919 929 937 941
947 953 967 971 977 983 991 997

下面是代码:

import java.util.Scanner;
class Demo03_18{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入一个数字:");
        int imax=scanner.nextInt();
        double sum=0;
        double flag=1;
        for(int i=1;i<=imax;i++){
            //sum+=Math.pow(-1.0,i+1);/(2*i-1);
            sum+=flag/(2*i-1);
            flag=-flag;
        }
        double pi=sum*4;
        System.out.println(pi);
        // 累乘 Math.pow(-1.0,i+1) for -1*-1*-1....
    }
}

输入一个数字看结果:

请输入一个数字:5
3.3396825396825403

下面是代码:

class Demo03_21{
    public static void main(String[] args){
        //6 : 1 2 3 4 5 6
        //28: 1 2 4 7 14 
        //n : 1 ~ n/2
        int sum=0;
        for(int n=2;n<=10000;n++){   
            for(int i=1;i<=n/2;i++){
                if(n%i==0){
                    sum+=i;
                }
            }
            if(n==sum){
                System.out.println("完全数"+n);
            }
            sum=0;
        }
    }
}
完全数6
完全数28
完全数496
完全数8128

下面是代码:

/*
12/2 6 ~ 0
6/2  3 ~ 0
3/2  1 ~ 1
1/2  0 ~ 1
1100
*/
import java.util.Scanner;
class Demo03_23{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入一个数字:");
        int num=scanner.nextInt();
        String binStr="";
        while(true){
            binStr=num%2+binStr;//"1100"
            num/=2;
            if(num==0){
                break;
            }
        }
        System.out.println(binStr);
    }
}

请输入一个数字:33
100001
发布了6 篇原创文章 · 获赞 0 · 访问量 41

猜你喜欢

转载自blog.csdn.net/weixin_45042315/article/details/104265959