2014-04-11 61 views
0

我已經看過幾個不同的堆棧問題和Google搜索,但我沒有看到真正處理整數的逆轉,但只是字符串。整數的逆轉檢查迴文數

所以現在我的代碼可能根本不可能工作,它可能是你見過的最愚蠢的東西,沒關係,並且更正很受歡迎,但是從我希望我的代碼將要做的事開始通過100 - 999乘以兩個整數,然後檢查它是否是迴文。如果與reverse.equals(總和)是完全僞代碼,顯然不會工作,但我無法弄清楚如何檢查迴文int。有沒有簡單的方法來做到這一點?我已經閱讀了一些非常冗長和複雜的方法,但是我確信有一個簡單的方法。也許不會。 :/。無論如何,這是我的代碼。

public class PalandromicNum { 
    public static void main(String[] args){ 
     int numOne = 100; 
     int numTwo = 100; 
     int toteVal; 
     int counter = 1000; 
     int sum = 0; 
     int finalSum = 0; 

     for(int i=0; i<counter; i++){ 
      toteVal = numOne * numTwo; 
      numTwo++; 

      if(numTwo == 999){ 
       numOne++; 
       numTwo = 100; 
      } 

      if(toteVal < sum){ 
       sum += toteVal; 
       if(reverse.equals(sum)){ 
        finalSum = sum; 
        System.out.println(finalSum); 
       } 
      } 
     } 
    } 
} 

再次提前致謝!

+0

提示:palindromes與值無關,但代表它們的字符。你如何「串起」字符? – indivisible

+0

雅,我想我可以把int轉換成一個字符串,我想我只是想看看我能用它做什麼,而不用做任何轉換。 – Kristaphonie

+0

只需將'Integer.toString()'掛接到http://stackoverflow.com/questions/4138827/check-string-for-palindrome –

回答

0

反向整數很容易。記住mod 10給你最後一位數字。循環播放,一次刪除最後一位數字,並將其添加到新數字。然後它的事情簡單integer equality

int rev = 0; 
int n = sum; 
while(n) 
{ 
    rev = rev*10 + n%10; 
    n /= 10; 
} 
if(sum==rev) 
    //palindrome 
else 
    //no no no no. 
2

這是在我的手機上,所以很抱歉有任何錯誤。

轉換你的電話號碼爲String和:

public static boolean isPalindrome(String str) 
{ 
    // base recursive case 
    if (str.length <= 1) { 
     return true; 
    } 
    // test the first and last characters 
    char firstChar = str.charAt(0); 
    char lastChar = str.charAt(str.length - 1) // subtract 1 as indexes are 0 based 
    if (!firstChar.equals(lastChar)) { 
     return false; 
    } 
    // if the string is longer than 2 chars and both are equal then recursively call with a shorter version 
    // start at 2nd char, end at char before last 
    return isPalindrome(str.substring(1,str.length); 
} 
0

您可以創建一個名爲isPalindrome函數來檢查一個數是否爲迴文。 在您的代碼中使用此功能。 你只需要將你想檢查的號碼傳入這個功能。 如果結果爲真,則數字爲迴文。 否則,它不是迴文。

public static boolean isPalindrome(int number) { 
      int palindrome = number; // copied number into variable 
      int reverse = 0; 

      while (palindrome != 0) { 
       int remainder = palindrome % 10; 
       reverse = reverse * 10 + remainder; 
       palindrome = palindrome/10; 
      } 

      // if original and reverse of number is equal means 
      // number is palindrome in Java 
      if (number == reverse) { 
       return true; 
      } 
      return false; 
     } 

    } 
0

我相信,如果你想知道有多少個迴文數在100-999之間,這段代碼應該可以幫助你。當然,它會對迴文序列進行兩次計數,因爲它考慮了迴文序列的兩個排列。如果我是你,我會開始創建方法來完成大部分工作,因爲它使調試變得更容易。

int total = 100; 
    StringBuilder stringSumForward; 
    StringBuilder stringSumBackward; 
    int numberOfPals = 0; 


    for(int i = 100; i < 999; i++){ 
    for(int j = 100; j < 999; j++){ 
     total = i * j; 
     stringSumForward = new StringBuilder(String.valueOf(total)); 
     stringSumBackward = new StringBuilder(String.valueOf(total)).reverse(); 

     if(stringSumForward.toString().equals(stringSumBackward.toString())){ 
      numberOfPals++; 
     } 
    } 
    }