12 classic Java algorithm problems that 90% of programmers can't solve (with reference code)

Even if you do web development, you will encounter various algorithm problems that need to be solved. This paper extracts some classic hand training algorithms and provides relevant reference answers, hoping to be helpful to you

In addition, I have collated and collected interview knowledge points from more than 20 companies for more than 20 years, and various Java core knowledge points are shared to you for free. I think it is very useful for the interview. If you want information, please like it and forward it to me [Java] to receive it.

[procedure 1]

Title: classical question: there is a pair of rabbits. They give birth to a pair of rabbits every month from the third month after birth. When the little rabbits grow to the third month, they give birth to another pair of rabbits every month. If the rabbits do not die, what is the total number of rabbits every month// This is a fiboracci sequence problem

public class test01 {
  public static void main(String[] args) {
   int f1=1,f2=1,f;
   int M=30;
   System.out.println(1);
   System.out.println(2);
 for(int i=3;i<m;i++) {
  f=f2;
  f2=f1+f2;
  f1=f;
  System.out.println(f2);
     }
   }
  }

[procedure 2]

Topic: judge how many primes there are between 101-200 and output all primes. Program analysis: the method of judging prime numbers: use a number to remove 2 to sqrt (this number) respectively. If it can be divided, it indicates that this number is not a prime number, on the contrary, it is a prime number.

public class test02 {
 public static void main(String[] args) {
  int count=0;
  for(int i=101;i<200;i+=2) {
   boolean flag=true;
    for(int j=2;j<=Math.sqrt(i);j++) {
       if(i%j==0) {
         flag=false;break;
         }
    }if(flag==true) {
        count++;
        System.out.println(i);
        }
      } 
    System.out.println(count);
    }
   }

[procedure 3]

Title: print out all the "daffodils". The so-called "daffodils" refers to a three digit number, and the sum of each digit cube is equal to the number itself. For example, 153 is a "daffodil number", because 153 = the third power of 1 + the third power of 5 + the third power of 3.

public class test03 {
   public static void main(String[] args) {
          int a,b,c;
          for(int i=101;i<1000;i++){
             a=i%10;
             b=i/10%10;
             c=i/100;
             if(a*a*a+b*b*b+c*c*c==i)
             System.out.println(i);
             }
          }
     }

[procedure 4]

Topic: decompose a positive integer into prime factors. For example, enter 90 and print out 90 = 233 * 5. Program analysis: to decompose the prime factor of N, you should first find a minimum prime number k, and then complete it according to the following steps: (1) if this prime number is exactly equal to N, it indicates that the process of decomposing the prime factor has ended, and you can print it out. (2) If n < > k, but n can be divided by K, print out the value of K, divide n by the quotient of K as a new positive integer, and repeat the first step. (3) If n cannot be divided by K, use k+1 as the value of K and repeat the first step.

import java.util.Scanner;
public class test04 {
      public static void main(String[] args) {
             Scanner input=new Scanner(http://System.in);
             int n=input.nextInt();
             int k=2;
             while(n>=k) {
                  if(n==k) {
                     System.out.println(k);
                     break;
                   }else if (n%k==0) {
                       System.out.println(k);
                       n=n/k;
                    }else {
                    k++;
                    }
                  }
             }
       }

[procedure 5]

Title: use the nesting of conditional operators to complete this question: students with academic achievement > = 90 points are represented by A, those between 60-89 points are represented by B, and those below 60 points are represented by C.

import java.util.Scanner;
public class test05 {
       public static void main(String[] args) {
              Scanner input=new Scanner(http://System.in);
              int score=input.nextInt();
              char grade=score>=90?'A':score>=60?'B':'C';
              System.out.println(grade);
       }
 }

[procedure 6]

Title: enter two positive integers m and n to find their maximum common divisor and minimum common multiple/* In the cycle, as long as the divisor is not equal to 0, divide the larger number by the smaller number, take the smaller number as the larger number of the next cycle, and the remainder obtained as the smaller number of the next cycle. In this way, the cycle returns the larger number until the value of the smaller number is 0. This number is the maximum common divisor, and the minimum common multiple is the product of two numbers divided by the maximum common divisor/

import java.util.Scanner;
public class test06 {
       public static void main(String[] args) {
              Scanner input =new Scanner(http://System.in);
              int a=input.nextInt();
              int b=input.nextInt();
              test06 test=new test06();
              int i = test.gongyinshu(a, b);
              System.out.println("Least common factor"+i);
              System.out.println("Maximum common multiple"+a*b/i);
              }
        public int gongyinshu(int a,int b) {
               if(a<b) {
                 int t=b;
                 b=a;
                 a=t;
                 }while(b!=0) {
                        if(a==b)
                           return a;
                           int x=b;
                           b=a%b;
                           a=x;
                           }
                   return a;
                 }
         }

[procedure 7]

Title: find the value of s=a+aa+aaa+aaaa+aa... A, where a is a number. For example, 2 + 22 + 222 + 2222 + 22222 (at this time, a total of 5 numbers are added), and the addition of several numbers is controlled by the keyboard.

import java.util.Scanner;
public class test07 {
       public static void main(String[] args) {
              Scanner input=new Scanner(http://System.in);
              int a=input.nextInt();
              int n=input.nextInt();
              int sum=0,b=0;
              for(int i=0;i<n;i++) {
                 b+=a;
                 sum+=b;
                 a=a*10;
                 }
              System.out.println(sum);
          }
  }

[procedure 8]

Title: if a number is exactly equal to the sum of its factors, the number is called "perfect". For example, 6 = 1 + 2 + 3 Program to find all completions within 1000.

public class test08 {
       public static void main(String[] args) {
              for(int i=1;i<=1000;i++) {
                  int t = 0;
                  for(int j=1;j<=i/2;j++) {
                     if(i%j==0) {
                        t+=j;
                        }
                   }if(t==i) {
                      System.out.println(i);
                   }
                }
         }
}

[procedure 9]

Title: there are four numbers: 1, 2, 3 and 4. How many three digits that are different from each other and have no duplicate numbers in a number? And enter them all.

public class test09 {
       public static void main(String[] args) {
              int count=0;
              for(int i=1;i<5;i++) {
                  for(int j=1;j<5;j++) {
                      for(int k=1;k<5;k++) {
                          if(i!=j&&j!=k&&i!=k) { 
                          count++;
                          System.out.println(i*100+j*10+k);
                          }
                       }
                   }
                }
             System.out.println(count);
         }
  }

[procedure 10]

Title: enter three integers x, y and Z. please output these three numbers from small to large.

import java.util.Scanner;
public class test10 {
       public static void main(String[] args) {
              Scanner input=new Scanner(http://System.in);
              int x=input.nextInt();
              int y=input.nextInt();
              int z=input.nextInt();
              int t=0;
              if(x>y) {
                t=x;
                x=y;y=t;
              }if(y>z) {
                 t=z;
                 z=y;
                 y=t;
              }if(x>y) {
                 t=x;
                 x=y;
                 y=t;
               }
              System.out.println(x+""+y+""+z)
         }
  }

[procedure 11]

Topic: monkeys eat peaches problem: on the first day, monkeys picked several peaches and ate half of them immediately. They were not addicted. They ate one more. The next morning, they ate half of the remaining peaches and one more. After that, I ate the remaining half and one every morning. When I wanted to eat again in the morning of the 10th day, I saw that there was only one peach left. Ask how much you picked on the first day.

public class test11 {
       public static void main(String[] args) {
             int x=1;
             for(int i=10;i>1;i--) {
                x=(x+1)*2;
             }
       System.out.println(x);
       }
}

[procedure 12]

Title: print out the pattern (diamond)

public class lingxi12 {
       public static void main(String[] args) {
              int H = 7, W = 7;//Height and width must be equal odd numbers
              for(int i=0; i<(H+1) / 2; i++) {
                 for(int j=0; j<wSystem.out.print(" ");}
                 for(int k=1; k<(i+1)*2; k++) {
                     System.out.print('*');
                 }
                 System.out.println();
              }for(int i=1; i<=H/2; i++) {
                   for(int j=1; j<=i; j++) {
                       System.out.print(" ");
                   }
                   for(int k=1; k<=W-2*i; k++) {
                       System.out.print('*');
                   }
               System.out.println();
               }
       }
  }

last

I have collected the notes of Java interview questions and various Java core knowledge points to share with you for free. I think it is very useful for the interview. If you want information, please like it and forward it to me [Java] to receive it. The following figure is a screenshot of the data

Keywords: Java Algorithm Interview Programmer

Added by Capoeirista on Wed, 23 Feb 2022 07:54:56 +0200