如果字串或數(shù)字顛倒後仍保持不變,則稱為回文。例如,“MADAM”是一個回文字串,因為即使顛倒過來也會拼寫為“MADAM”。但在“LUCKY”的情況下,該字串不是回文,因為反轉(zhuǎn)後它是“YKCUL”。一些回文數(shù)是 365563、48984、12321、171、88、90009、343,一些回文字串是 MADAM、MALAYALAM、LOL、DAD、MOM、C++&++C 等。 接下來讓我們來看看回文的邏輯和實作。在本主題中,我們將學習 Java 中的回文。
開始您的免費軟體開發(fā)課程
網(wǎng)頁開發(fā)、程式語言、軟體測試及其他
Java 中回文背後的邏輯
為了檢查一個數(shù)字是否為回文數(shù),可以使用以下演算法。
- 輸入字串或數(shù)字,需要檢查它是否是回文。
例如,讓我們輸入數(shù)字 353。
- 取得輸入數(shù)字並將其複製到臨時變數(shù)
353-> temp
- 使用 for、while 或您選擇的任何方法反轉(zhuǎn)它。
Reversednumber: rev=353
- 比較輸入的數(shù)字和反轉(zhuǎn)的數(shù)字。
如果它們相同,則該數(shù)字稱為回文數(shù)。
否則,該數(shù)字不是回文數(shù)。
即
If(inputnum==rev) { then palindrome } Else not palindrome
如何使用各種方法測試回文?
有幾種方法可以檢查給定的輸入數(shù)字是否為回文。
- For 循環(huán)
- While 循環(huán)
- 函式庫方法(用於字串)
讓我們詳細研究每一個:
1.使用for迴圈檢查回文數(shù)的程式
代碼:
//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 顛倒後仍然不一樣,因此不被視為回文。
2.使用 While 迴圈檢查回文數(shù)的程式
代碼:
//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:
3.使用函式庫方法檢查回文數(shù)的程式(對於字串)
代碼:
//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"); } } }
輸出:
結(jié)論
如果一個數(shù)字即使顛倒也保持不變,則稱為回文數(shù)。回文也可以在字串中檢查?;匚臄?shù)和字串有 MOM、MALAYALAM、DAD、LOL、232、1331 等。本文檔涵蓋了回文數(shù)的幾個方面,如演算法、方法、實作等。
以上是Java 中的回文的詳細內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

Undresser.AI Undress
人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Laravel支持使用原生SQL查詢,但應(yīng)優(yōu)先使用參數(shù)綁定以確保安全;1.使用DB::select()執(zhí)行帶參數(shù)綁定的SELECT查詢,防止SQL注入;2.使用DB::update()執(zhí)行UPDATE操作並返回影響行數(shù);3.使用DB::insert()插入數(shù)據(jù);4.使用DB::delete()刪除數(shù)據(jù);5.使用DB::statement()執(zhí)行如CREATE、ALTER等無結(jié)果集的SQL語句;6.推薦在QueryBuilder中使用whereRaw、selectRaw等方法結(jié)合原生表達式以提升安

使用JUnit5和Mockito能有效隔離依賴進行單元測試,1.通過@Mock創(chuàng)建模擬對象,@InjectMocks注入被測實例,@ExtendWith啟用Mockito擴展;2.使用when().thenReturn()定義模擬行為,verify()驗證方法調(diào)用次數(shù)與參數(shù);3.可模擬異常場景並驗證錯誤處理;4.推薦構(gòu)造函數(shù)注入、避免過度模擬、保持測試原子性;5.使用assertAll()合併斷言,@Nested組織測試場景,從而提升測試可維護性和可靠性。

Go泛型從1.18開始支持,用於編寫類型安全的通用代碼。 1.泛型函數(shù)PrintSlice[Tany](s[]T)可打印任意類型切片,如[]int或[]string。 2.通過類型約束Number限制T為int、float等數(shù)字類型,實現(xiàn)Sum[TNumber](slice[]T)T安全求和。 3.泛型結(jié)構(gòu)體typeBox[Tany]struct{ValueT}可封裝任意類型值,配合NewBox[Tany](vT)*Box[T]構(gòu)造函數(shù)使用。 4.為Box[T]添加Set(vT)和Get()T方法,無需

table-layout:fixed會強製表格列寬由第一行單元格寬度決定,避免內(nèi)容影響佈局。 1.設(shè)置table-layout:fixed並指定表格寬度;2.為第一行th/td設(shè)置具體列寬比例;3.配合white-space:nowrap、overflow:hidden和text-overflow:ellipsis控製文本溢出;4.適用於後臺管理、數(shù)據(jù)報表等需穩(wěn)定佈局和高性能渲染的場景,能有效防止佈局抖動並提升渲染效率。

json.loads()用於將JSON字符串解析為Python數(shù)據(jù)結(jié)構(gòu),1.輸入必須是雙引號包裹的字符串且布爾值為true/false;2.支持null→None、對象→dict、數(shù)組→list等自動轉(zhuǎn)換;3.常用於處理API返回的JSON字符串,如response_string經(jīng)json.loads()解析後可直接訪問嵌套數(shù)據(jù),使用時需確保JSON格式正確,否則會拋出異常。

Choosetheappropriateindextypebasedonusecase,suchassinglefield,compound,multikey,text,geospatial,orTTLindexes.2.ApplytheESRrulewhencreatingcompoundindexesbyorderingfieldsasequality,sort,thenrange.3.Designindexestosupportcoveredqueriesbyincludingallque

Maven是Java項目管理和構(gòu)建的標準工具,答案在於它通過pom.xml實現(xiàn)項目結(jié)構(gòu)標準化、依賴管理、構(gòu)建生命週期自動化和插件擴展;1.使用pom.xml定義groupId、artifactId、version和dependencies;2.掌握核心命令如mvnclean、compile、test、package、install和deploy;3.利用dependencyManagement和exclusions管理依賴版本與衝突;4.通過多模塊項目結(jié)構(gòu)組織大型應(yīng)用並由父POM統(tǒng)一管理;5.配

Python中函數(shù)傳參是“傳遞對象引用”,即1.對於可變對象(如列表、字典),函數(shù)內(nèi)進行原地修改(如append、賦值切片)會直接影響原對象;2.對於不可變對象(如整數(shù)、字符串),函數(shù)內(nèi)無法改變原對象,重新賦值只會創(chuàng)建新對象;3.參數(shù)傳遞的是引用的副本,若在函數(shù)內(nèi)重新綁定變量(如lst=[...]),則斷開與原對象的連接,不影響外部變量。因此,修改可變對象會影響原數(shù)據(jù),而不可變對象和重新賦值則不會,這解釋了為何列表在函數(shù)內(nèi)修改後外部可見,而整數(shù)變化僅限局部。
