如果字串或數字顛倒後仍保持不變,則稱為回文。例如,“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中文網其他相關文章!