判断回文数字

2018-12-03  本文已影响0人  静水流深ylyang

版权声明:本文为博主原创文章,转载请注明出处。
个人博客地址:https://yangyuanlin.club
欢迎来踩~~~~


#include<iostream>
using namespace std;
// 方法一
bool isPalindrome(int x)
{
    if(x<0 || x%10==0&&x!=0)return false;
    long y = x;
    long temp=0;
    while(y>0)
    {
        temp = temp*10+y%10;
        y/=10;
    }
    return temp==x;
}
int main()
{
    int x;
    cin>>x;
    if(isPalindrome(x))
    {
        cout<<"是回文数字"<<endl;
    }
    else
    {
        cout<<"不是回文数字"<<endl;
    }
    return 0;
}
    //方法二
    bool isPalindrome(int x)
    {
        if(x<0)return false;
        char ch[100];
        int k=0;
        while(x>0)
        {
            ch[k++] = char(x%10+'0');
            x/=10;
        }
        ch[k]='\0';
        k--;
        for(int i=0;i<k;i++,k--)
        {
            if(ch[i]!=ch[k])return false;
        }
        return true;
    }

版权声明:本文为博主原创文章,转载请注明出处。
个人博客地址:https://yangyuanlin.club
欢迎来踩~~~~


上一篇 下一篇

猜你喜欢

热点阅读