The collection of Java basic brushing exercises is continuously updated (judging prime numbers, daffodil numbers, Fibonacci sequence recursion, leap year, upper and lower case letters, sequential search, half search)

foreword

This article mainly collects several common basic programming questions, which are suitable for students who have just learned the basics of Java or reviewed Java grammar for practice. This column will update the knowledge related to Java algorithms later, if you are interested, you can subscribe and pay attention~

judging prime number

A prime number, also known as a prime number, refers to a number greater than 1 that cannot be divisible by other natural numbers except 1 and the integer itself.

Check if a number is prime:

import java.util.*;
//判断一个数是不是素数
public class Main {
    
    
	public static void main(String[] args) {
    
    
		Scanner s = new Scanner(System.in);
		int a = s.nextInt();
		int k= 0;
		for(int i=2;i<a;i++) {
    
    
			if(a%i==0) {
    
    
				k++;
			}
		}
		if(k==0) {
    
    
			System.out.println("是");
		} else {
    
    
			System.out.println("不是");
		}
	}
}

daffodil number

A daffodil number is a 3-digit number whose sum to the power of 3 is equal to itself (for example: 1^3 + 5^3+ 3^3 = 153).

Determine if a three-digit number is a daffodil number:

import java.util.*;
public class Main {
    
    
	public static void main(String[] args){
    
    
		System.out.println("请输入一个数字:");
		Scanner scanner = new Scanner(System.in);
		int num = scanner.nextInt();
		int a = num/100; // 百位
		int b = num/10%10; // 十位
		int c = num%100%10; // 个位
		System.out.println(a+""+b+""+c);
		if(a*a*a+b*b*b+c*c*c == num) {
    
    
			System.out.println("是水仙花数");
		} else {
    
    
			System.out.println("不是水仙花数");
		}
	}
}

Find the three-digit number of all daffodils:

import java.util.*;
public class Main {
    
    
	public static void main(String[] args){
    
    
		for(int num=100;num<=999;num++) {
    
    
			int a = num/100;
			int b = num/10%10;
			int c = num%100%10;
			if(a*a*a+b*b*b+c*c*c == num) {
    
    
				System.out.println(num);
			}
		}
	}
}

Fibonacci sequence

F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 2,n ∈ N*)

Find the nth number in the Fibonacci sequence:

  1. Implement using recursion
import java.util.*;
public class Fibonacci {
    
    
	public static void main(String[] args) {
    
    
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		fib(a);
		System.out.println(fib(a));
	}
	public static int fib(int a) {
    
    
		if (a <= 2) {
    
    
			return 1;//前两项都为1,所以直接返回1即可
		}
		return fib(a - 1) + fib(a - 2);
	}
}
  1. Implemented using a loop:
import java.util.*;
public class Fibonacci {
    
    
	public static void main(String[] args) {
    
    
		Scanner scanner = new Scanner(System.in);
		int s = scanner.nextInt();
		int a = 1,b=1,c=0;
		for(int i=3;i<=s;i++) {
    
    
			c = a+b;
			a = b;
			b = c;
		}
		System.out.println(c);
	}
}

Determine leap year:

Enter the year to determine whether it is a leap year:

import java.util.*;
public class Main {
    
    
	public static void main(String[] args) {
    
    
		Scanner s = new Scanner(System.in);
		int year = s.nextInt();
		if(year%4==0 && year%100 !=0 || year%400==0) {
    
    
			System.out.println("是闰年");
		} else {
    
    
			System.out.println("是平年");
		}
	}
}

Determine uppercase and lowercase letters:

Enter letters and judge case:
you can also use ascii code to judge

import java.util.*;
public class Main {
    
    
	public static void main(String[] args) {
    
    
		Scanner s = new Scanner(System.in);
		char a = s.nextLine().charAt(0);
		if(a>='a' && a<='z') {
    
    
			System.out.println("是小写字母");
		}else if(a>='A' && a<='Z'){
    
    
			System.out.println("是大写字母");
		}else {
    
    
			System.out.println("其他");
		}
	}
}

find element in sorted array

Sequential lookup:

import java.util.*;
public class Main {
    
    
	public static void main(String[] args) {
    
    
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		int[] arr = new int[]{
    
    1,2,3,4,5};
		int k = 0;
		for(int i=0;i<arr.length;i++) {
    
    
			if(a == arr[i]) {
    
    
				k++;
			}
		}
		if(k!=0) {
    
    
			System.out.println("找到啦");
		}else {
    
    
			System.out.println("不存在");
		}
	}
}

Binary search (half search):

public class Main {
    
    
	public static void main(String[] args) {
    
    
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		int[] arr = new int[]{
    
    1,2,3,4,5};
		int k = 0;
		int start = 0;
		int end = arr.length-1;
		while(start<=end) {
    
    
			int mid = (start+end)/2;
			if(a<arr[mid])
				end = mid-1;
			else if(a>arr[mid])
				start = mid+1;
			else {
    
    
				k++;
				break;
			}
		}
		if(k!=0) {
    
    
			System.out.println("找到啦");
		}else {
    
    
			System.out.println("不存在");
		}
	}
}

at last

This article will continue to be updated. I will add those who are more basic and suitable for practice. I will continue to update java algorithm programming in the future. The difficulty will gradually increase. This article is only suitable for basic practice. If you are interested, you can pay attention~~~

Guess you like

Origin blog.csdn.net/weixin_45745641/article/details/128336507