判断输入否为回文

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

using namespace std;

    bool isPalindrome(int x) 
  {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(x<0)
           return 0;
        char c[100];
        memset(c, '\0', 100);
        sprintf(c, "%d", x);
        
        int i = 0, j = strlen(c)-1;
        for(; i<=j; i++, j--)
        {
            if(c[i] != c[j])
               return 0;
        }
        return 1;
    }


  
int main (int argc, char *argv[])
{
    cout<<isPalindrome(100)<<endl;
}


bool isPalindrome(int x) {
  if (x < 0) return false;
  int div = 1;
  while (x / div >= 10) {
    div *= 10;
  }
  while (x != 0) {
    int l = x / div;
    int r = x % 10;
    if (l != r) return false;
    x = (x % div) / 10;
    div /= 100;
  }
  return true;
}


方法一是将整数转化为字符串判断

方法二是直接进行判断  ,负数肯定不是回文




如果输入的直接是字符串 那就更好办了 

bool ispalidrome(string str)
{
	string::size_type len=str.size();
	for(string::size_type i=0;i!=len/2;i++)
	{
	if(str[i]!=str[len-1-i])
		return false;
	}

return  true;
}
void main()
{
cout<<ispalidrome("1232")<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值