章の演習(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行の数、ルック法則を出す:
 。4. 3. 3. 4 2 2 1
-3 -2 -2 0 2 1 3
、ライン4x∈[-3,3] Y = | X | +1
5 OKx∈[-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を
この数をm NUM%メートル== 0 numは素数でない場合は2〜メートル〜NUM-1は、番号を検索し
、その後、1はこの番号を見つけることができない場合素数であります

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