Number of palindromes per day for a small exercise (two methods)

Title description: Enter an integer n, please check whether it is a "palindrome number"

Number of palindromes: numbers consistent with both forward and reverse
For example:
input: 12321
return: 1 represents the number of palindromes

Input: 9527
Return: 0 means it is not a palindrome

Idea One

We have to input an integer, and then determine whether the integer is a palindrome, if it is a palindrome, output 1, if not output 0;
then we must first solve the input problem

  1. Create a Scanner class object input, and use the nextInt() method in the class to get the number entered by the keyboard.
  2. Next, we should judge whether it is a palindrome number. How to judge it? Isn't that the comparison between the first and last digit of this number and whether the second digit is the same with the penultimate digit? By analogy, if there is a difference, there is no need to compare it later, it is directly not a palindrome number, and 0 is output.
  3. At this time, we don’t know how many digits this integer has, so it’s not very sure when it will end. So we switch our thinking. We can use this integer (9527) and its inverted number (7259). ) Compared, if it is different, it means that this number is not a palindrome number.
  4. To reverse the integer, we should first convert the int integer type to the String string type, so that each digit of this number can be easily obtained through the (charAt()) method in the String class. Then put it in reverse order into another string.
  5. For comparison, here are a variety of ideas:
  • Directly use the ( equals() ) method in the String class combined with the ternary operator (or use if...else... to judge)
  • Convert to integer Int type, use (==) to judge
import java.util.Scanner;

public class Test02 {
    
    
	public static void main(String[] args) {
    
    
		// 1. 获取输入的数字
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();

		// 2. 将数字转换为字符串
		String strNumber = String.valueOf(n);

		String result = ""; // 用于保存新数字的字符串

		// 3.倒序遍历每个字符,组成新数字
		for (int i = strNumber.length() - 1; i >= 0; i--) {
    
    
			result += strNumber.charAt(i);
		}

		//System.out.println(result.equals(strNumber) ? 1 : 0);

		if (result.equals(strNumber)) {
    
    
			System.out.println(1);
		} else {
    
    
			System.out.println(0);
		}

	}
}

Idea two

Similarly, when we solve the input, we think that all the numbers less than 0 are not palindrome numbers, so we use mathematical methods to reverse the numbers. Then judge whether they are the same.
Mathematical thinking: use 12321 as an example

  • i: 12321%10=1 (get the last number and save it in the temporary variable temp)
  • ii: To store the last number obtained in revese, the total number in revese needs to be x10, and then +temp; at this time revese=1;
  • iii: In addition, delete the last digit in the original number, then num=num÷10; at this time, num=1232;
  • Then repeat the first step: temp=1232%10=2;
  • Repeat the second step: revese=revese×10+temp=1×10+2=12;
  • Repeat the third step: num=num÷10=123;
  • Then repeat the first step: temp=123%10=3;
  • Repeat the second step: revese=revese×10+temp=12×10+3=123;
  • Repeat the third step: num=num÷10=12;
  • …Loop in turn, let’s use the while loop and
    look at the code directly!
import java.util.Scanner;

public class Program002 {
    
    
	public static void main(String[] args) {
    
    
		// 1. 获取输入的数字
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();

		// 2.判断是否是回文数
		System.out.println(isPalindromeNumber(n));
	}
	
	public static int isPalindromeNumber(int number) {
    
    
		
	        if(number<0)return 0;
	        int revese=0,num=number;
	        while(num!=0){
    
    
	        	int temp=num%10;
	            revese=revese*10+temp;
	            num/=10;
	            
	        }
	      
	       return number==revese ? 1:0;
	
	}
}

Guess you like

Origin blog.csdn.net/weixin_51529267/article/details/112814999