如果字符串或数字颠倒后仍保持不变,则称其为回文。例如,“MADAM”是一个回文字符串,因为即使颠倒过来也会拼写为“MADAM”。但在“LUCKY”的情况下,该字符串不是回文,因为反转后它是“YKCUL”。一些回文数是 365563、48984、12321、171、88、90009、343,一些回文字符串是 MADAM、MALAYALAM、LOL、DAD、MOM、C++&++C 等。 接下来让我们看看回文的逻辑和实现。在本主题中,我们将学习 Java 中的回文。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
为了检查一个数字是否是回文数,可以使用以下算法。
例如,让我们输入数字 353。
353-> temp
Reversednumber: rev=353
如果它们相同,则该数字被称为回文数。
否则,该数字不是回文数。
即
If(inputnum==rev) { then palindrome } Else not palindrome
有几种方法可以检查给定的输入数字是否是回文。
让我们详细研究每一个:
代码:
//Java program to check whether a String is a Palindrome or not using For Loop import java.util.*; public class PalindromeNumberExample { //main method public static void main(String[] args) { int r=0 ; //reversed Integer int rem, num; //remainder and original number Scanner s = new Scanner(System.in); System.out.print("Enter number that has to be checked:"); num = s.nextInt(); //Store the number in a temporary variable int temp = num; //loop to find the reverse of a number for( ;num != 0; num /= 10 ) { rem = num % 10; // find the modulus of the number when divided by 10 r = r * 10 + rem; } //check whether the original and reversed numbers are equal if (temp == r) { System.out.println(temp + " is input number"); System.out.println(r + " is the reversed number"); System.out.println("Since they are equal " + temp + " is a palindrome number"); } else { System.out.println(temp + " is input number"); System.out.println(r + " is the reversed number"); System.out.println("Since they are not equal " + temp + " is not a palindrome number"); } } }
输出 1:
这里,由于353颠倒后是一样的,所以被认为是回文。
输出 2:
这里,由于 234 颠倒后仍然不一样,因此不被视为回文。
代码:
//Java program to check whether a number is a Palindrome or not using While Loop import java.util.*; public class PalindromeNumberExample { public static void main(String[] args) { int r=0, rem, num; Scanner s = new Scanner(System.in); System.out.print("Enter number that has to be checked:"); num = s.nextInt(); //Store the number in a temporary variable int temp = num; //loop to find the reverse of a number while( num != 0 ) { rem= num % 10; r= r * 10 + rem; num=num/10; } //check whether the original and reversed numbers are equal if (temp == r) { System.out.println(temp + " is input number"); System.out.println(r + " is the reversed number"); System.out.println("Since they are equal " + temp + " is a palindrome number"); } else { System.out.println(temp + " is input number"); System.out.println(r + " is the reversed number"); System.out.println("Since they are not equal " + temp + " is not a palindrome number"); } } }
输出 1:
输出 2:
代码:
//Java program to check whether a String is a Palindrome or not using Library method import java.util.*; public class PalindromeNumberExample { //Function to check whether the string is palindrome or not public static void PalindromeCheck(String str) { // reverse the input String String rev = new StringBuffer(str).reverse().toString(); // checks whether the string is palindrome or not if (str.equals(rev)) { System.out.println("input string is :" + str); System.out.println("Reversed string is :" + rev); System.out.println("Since the input and reversed string are equal, "+ str +" is a palindrome"); } else { System.out.println("input string is :" + str); System.out.println("Reversed string is :" + rev); System.out.println("Since the input and reversed string are not equal, "+ str +" is not a palindrome"); } } public static void main (String[] args) { PalindromeCheck("MALAYALAM"); } }
输出:
这里,输入字符串是在程序本身中传递的。
要检查字符串是否是回文,还可以使用以下程序。
代码:
//Java program to check whether a String is a Palindrome or not import java.util.*; public class PalindromeNumberExample { public static void main(String args[]) { String st, rev = ""; Scanner sc = new Scanner(System.in); System.out.println("Enter the string that has to be checked:"); st = sc.nextLine(); int len = st.length(); //length of the string for ( int i = len- 1; i >= 0; i-- ) rev = rev + st.charAt(i); if (st.equals(rev)) { System.out.println("input string is :" + st); System.out.println("Reversed string is :" + rev); System.out.println("Since the input and reversed string are equal, "+ st +" is a palindrome"); } else { System.out.println("input string is :" + st); System.out.println("Reversed string is :" + rev); System.out.println("Since the input and reversed string are not equal, "+ st +" is not a palindrome"); } } }
输出:
如果一个数字即使颠倒也保持不变,则称为回文数。回文也可以在字符串中检查。回文数和字符串有 MOM、MALAYALAM、DAD、LOL、232、1331 等。本文档涵盖了回文数的几个方面,如算法、方法、实现等。
以上是Java 中的回文的详细内容。更多信息请关注PHP中文网其他相关文章!