简体   繁体   English

检查一个数字是否是回文

[英]Checking if a number is a palindrome

I've tried to check whether a number is a palindrome with the following code: 我试着通过以下代码检查数字是否是回文:

unsigned short digitsof (unsigned int x)
{
    unsigned short n = 0;
    while (x)
    {
        x /= 10;
        n++;
    }
    return n;
}

bool ispalindrome (unsigned int x)
{
    unsigned short digits = digitsof (x);

    for (unsigned short i = 1; i <= digits / 2; i++)
    {
        if (x % (unsigned int)pow (10, i) != x % (unsigned int)pow (10, digits - 1 + i))
        {
            return false;
        }
    }
    return true;
}

However, the following code isn't able to check for palindromes - false is always returned even if the number is a palindrome. 但是,以下代码无法检查回文 - 即使数字是回文,也始终返回false。

Can anyone point out the error? 有谁可以指出错误?

(Please note: I'm not interested to make it into a string and reverse it to see where the problem is: rather, I'm interested to know where the error is in the above code.) (请注意:我没有兴趣将它变成一个字符串并反转它以查看问题所在:相反,我很想知道上面代码中的错误在哪里。)

我个人只是从数字中构建一个字符串,然后将其视为正常的回文检查(检查前半部分中的每个字符是否与length()-index匹配的那些length()-index )。

The problem is this: 问题是这样的:

x % (unsigned int)pow (10, i)

Lets try: 我们试试吧:

x =504405
i =3

SO I want 4.

x % 10^3 => 504405 %1000 => 405 NOT 4

How about 怎么样

x / (unsigned int)pow (10, i -1) % 10

x % (unsigned int)pow (10, i)不是第i位。

Just for more info! 只是为了更多信息! The following two functions are working for me: 以下两个功能对我有用:

double digitsof (double x)
{
    double n = 0;
    while (x > 1)
    {       
        x /= 10;
        n++;
    }
    return n;
}

bool ispalindrome (double x)
{
    double digits = digitsof (x);
    double temp = x;
    for(double i = 1; i <= digits/2; i++)
    {
        float y = (int)temp % 10;
        cout<<y<<endl;
        temp = temp/10;

        float z = (int)x / (int)pow(10 , digits - i);
        cout<<(int)z<<endl;
        x = (int)x % (int)pow(10 , digits - i);

        if(y != z)
            return false;

    }
    return true;        
}

Code to check if given number is palindrome or not in JAVA 在JAVA中检查给定数字是否为回文的代码

import java.util.*;
public class HelloWorld{

private static int countDigits(int num) {
    int count = 0;
    while(num>0) {
        count++;
        num /= 10;
    }
    return count;
}
public static boolean isPalin(int num) {
    int digs = HelloWorld.countDigits(num);
    int divderToFindMSD = 1;
    int divderToFindLSD = 1;

    for (int i = 0; i< digs -1; i++)
    divderToFindMSD *= 10;

    int mid = digs/2;  

    while(mid-- != 0)
    {
        int msd = (num/divderToFindMSD)%10;
        int lsd = (num/divderToFindLSD)%10;
        if(msd!=lsd)
        return false;

        divderToFindMSD /= 10;
        divderToFindLSD *= 10;
    }
    return true;

}

 public static void main(String []args) {
    boolean isPalin = HelloWorld.isPalin(1221);
    System.out.println("Results: " + isPalin);
 }
 }

I have done this with my own solution which is restricted with these conditions 我用自己的解决方案做了这个,这个解决方案受到这些条件的限制

  1. Do not convert int to string. 不要将int转换为字符串。
  2. Do not use any helper function. 不要使用任何辅助功能。
var inputNumber = 10801

var firstDigit = 0

var lastDigit = 0

var quotient = inputNumber



while inputNumber > 0 {

    lastDigit = inputNumber % 10

    var tempNum = inputNumber

    var count = 0

    while tempNum > 0 {

        tempNum = tempNum / 10

        count = count + 1

    }

    var n = 1

    for _ in 1 ..< count {

        n = n * 10

    }

    firstDigit = quotient / n

    if firstDigit != lastDigit {

        print("Not a palindrome :( ")

        break

    }

    quotient = quotient % n

    inputNumber = inputNumber / 10

}

if firstDigit == lastDigit {

    print("It's a palindrome :D :D ")

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM