Chapter III summary of examples

Solutions:

Write a continuous number and figure out its reverse order

12345
12345%10=5 12345/10=1234
1234%10=4  1234/10=123
123%10=3   123/10=12
12%10=2    12/10=1
1%10=1     1/10=0
54321

Use a regular formula to express the reverse order
5*10000+4*1000+3*100+2*10+1
(5*1000+4*100+3*10+2)*10+1
((5*100+4*10+3)*10+2)*10+1
(((5*10+4)*10+3)*10+2)*10+1
((((0*10+5)*10+4)*10+3)*10+2)*10+1

sum=0
sum=sum*10+5;   //5
sum=sum*10+4;   //54
sum=sum*10+3;   //543
sum=sum*10+2;   //5432
sum=sum*10+1;   //54321

Code and output:

import java.util.Scanner;
class Demo03_04{
    public static void main(String[] args){
        //1. Enter a number
        Scanner scanner=new Scanner(System.in);
        System.out.print("Please enter a number:");
        int num=scanner.nextInt();
        int temp=num;
        //2. Splicing out the reverse order of the number
        int sum=0;
        sum=sum*10+num%10;
        num/=10;
        sum=sum*10+num%10;
        num/=10;
        sum=sum*10+num%10;
        num/=10;
        sum=sum*10+num%10;
        num/=10;
        sum=sum*10+num%10;
        num/=10;
        sum=sum*10+num%10;
        num/=10;
        if(sum==temp){
            System.out.println("It's palindrome.");
        }else{
            System.out.println("Not palindrome.");
        }
    }
}

 

 

Solutions:

First of all, we will list the categories that we need to define for this problem. They are: the coordinate distance of the radius point of the center circle of the circle

Here are the general steps to solve the problem: '

1. Prompt the user to enter the coordinates of the point
2. Calculate the distance from the point to the center of the circle
3. Judge the relationship between distance and radius
Distance > radius point outside circle
Distance = = radius point on circle
Distance < radius point in circle

Code and output:

import java.util.Scanner;
class Demo03_08{
    public static void main(String[] args){
        //1.
        Scanner scanner=new Scanner(System.in);
        System.out.print("Please enter coordinate point:");
        double x=scanner.nextDouble();
        double y=scanner.nextDouble();
        //2.
        double xo=0;
        double yo=0;
        double radius=10;
        double distance=Math.sqrt(Math.pow(x-xo,2)+Math.pow(y-yo,2));
        //3.
        if(distance>radius){
            System.out.println("Points outside the circle.");
        }else if(distance==radius){
            System.out.println("Points on the circle");
        }else{
            System.out.println("Points in the circle");
        }
    }
}

 

 Solving problems:

with(x1,y1)Draw a coordinate map for the origin,First draw the small rectangle and the four corners of the big rectangle

 

Then draw the situation that the small rectangle is outside the large rectangle

Code and output:

import java.util.*; 
class Demo03_10{    
    public static void main(String[] args){         
        Scanner scanner = new Scanner(System.in);         
        System.out.print("Please enter the center coordinates of the first rectangle x,y And length, height:");         
        double x1 = scanner.nextDouble();         
        double y1 = scanner.nextDouble();         
        double w1 = scanner.nextDouble();         
        double h1 = scanner.nextDouble();         
        System.out.print("Please enter the center coordinates of the second rectangle x,y And length, height:");         
        double x2 = scanner.nextDouble();         
        double y2 = scanner.nextDouble();         
        double w2 = scanner.nextDouble();         
        double h2 = scanner.nextDouble();         
        //Calculate the range of the second rectangular center point         
        double inMinX = x1-(w1-w2)/2;         
        double inMaxX = x1+(w1-w2)/2;         
        double inMinY = y1-(h1-h2)/2;         
        double inMaxY = y1+(h1-h2)/2;         
        double outMinX = x1-(w1+w2)/2;         
        double outMaxX = x1+(w1+w2)/2;         
        double outMinY = y1-(h1+h2)/2;         
        double outMaxY = y1+(h1+h2)/2;         
        //Determine whether it is inside or outside or nested         
        if(x2>=inMinX&&x2<=inMaxX&&y2>=inMinY&&y2<=inMaxY){             
            System.out.println("Included");         
            }else if(x2>=outMaxX||x2<=outMinX||y2>=outMaxY||y2<=outMinY){             
                System.out.println("Out of");         
                }else{             
                    System.out.println("nesting");        
             }     
         } 
     } 

 

Code:

class Demo03_12{
    /* 
    21  42
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16~21
    3 7
    */
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        //1. Enter two numbers
        System.out.print("Please enter two numbers:");
        int n1=scanner.nextInt();
        int n2=scanner.nextInt();
        int gcd=1;
        for(int i=n1<n2?n1:n2;i>=1;i--){
            if(n1%i==0&&n2%i==0){
                gcd=i;
                break;
            }
        }
        System.out.println(gcd);

    }
}

 

Train of thought:

120 2~120 ->2
60  2~60  ->2
30  2~30  ->2
15  2~15  ->3
5   2~5   ->5

Code:

import java.util.Scanner;
class Demo03_13{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("Please enter a number:");
        int num=scanner.nextInt();
        while(true){
            for(int i=2;i<=num;i++){
                if(num%i==0){
                    System.out.print(i+" ");
                    num=num/i;
                    break;
                }
            }
            if(num==1){
                break;
            }
        }
    }
}

 

Codes and tips

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("Perfect number"+n);
            }
            sum=0;
        }
    }
}

 

 

Code reference:

import java.util.Scanner;
class Demo03_24{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        int num=0;
        int max=0;
        int count=0;
        System.out.print("Please enter a number:");
        while(true){
            num=scanner.nextInt();
            if(num==0){
                break;
            }else if(num>max){
                max=num;
                count=1;
            }else if(num==max){
                count++;
            }
        }
        System.out.println("max="+max+",count="+count);
    }
}

 

Published 4 original articles, won praise 0, visited 34
Private letter follow

Tags: Java

Posted on Fri, 14 Feb 2020 11:36:02 -0500 by RichterBelmont