题目描述:
letCode刷题笔记1:
问题分析:

  1. 小于0的整型数字不是回文数;
  2. 以0结尾的整形数字不是回文数;
  3. 0到9的数字是回文数;
  4. 其他情况,下最暴力的方法是逐一分解,然后首尾对比;

完整代码:

#include<iostream>
using namespace std;
int array[10] = {0};

int spilt(int n)
{
	int i = 0;
	
	while (n != 0)
	{
		array[i++] = n % 10;
		n /= 10; 
	}
	
	return i-1;
}

bool isPalindrome(int n)
{	
	// 对简单情况预先处理 
	// 负数,结尾0 不是回文数
	//  [0-9]的数为回文数 
	if (n < 0 || (n % 10 == 0 && n != 0))
		return false;
	else if (n < 10)
		return true;
	
	// 其他情况下的处理办法
	// 拆分位数,首尾逐一对比,若有一对不相等则不是
	int i = 0, j = spilt(n);
	while (j - i >= 0 )
	{
		if (array[i++] != array[j--])
			return false;
	}
	
	return true;
			
}

int main()
{
	int n;
	while (cin >> n)
	{
		if (isPalindrome(n))
			cout << "true";
		else
			cout << "false";
	}
	
	return 0;	
} 

结果展示:
letCode刷题笔记1:

相关文章:

  • 2021-06-30
  • 2021-07-01
  • 2021-12-31
  • 2021-09-22
  • 2021-07-27
猜你喜欢
  • 2021-06-13
  • 2022-12-23
  • 2021-04-13
  • 2021-08-11
  • 2021-05-13
  • 2021-11-23
  • 2022-12-23
相关资源
相似解决方案