Palindrome Number

来源:互联网 发布:网站实时弹幕 源码 编辑:程序博客网 时间:2024/06/05 04:57

今天编写代码超没有感觉,本道题并不难,但在编写的过程中每敲一个字符,都感觉到违和感。路漫漫其修远兮,淡定,要坐得住。

#include<iostream>using namespace std;class Solution {public:    bool isPalindrome(int x) {if(x < 0)return false;int tmp = x;if(tmp >= 0 && tmp <= 9) {//cout << "is palindrome" << endl;//getchar();return true;}int tmp1 = tmp;int counter = 1;long long weight = 10;for(; ; ) {if(tmp1 / weight == 0)break;counter++;weight *= 10;}//cout << counter << endl;int startCounter;if(counter % 2 == 1)startCounter = (counter + 1) / 2;elsestartCounter = counter / 2;//cout << "startCounter:" << startCounter << endl;int comCounter;if(counter % 2 == 1)comCounter = (counter - 1) / 2;elsecomCounter = counter / 2;//cout << "comCounter:" << comCounter << endl;int bitHigh;int bitLow;int startCounterTmp = startCounter;for(;comCounter >= 1;comCounter--) {bitHigh = (tmp / (int)pow(10, startCounterTmp)) % 10;bitLow = (tmp / (int)pow(10, counter - 1 - startCounterTmp)) % 10;if(bitHigh != bitLow)break;startCounterTmp++;}//cout << "comCounter:" << comCounter << endl;if(comCounter != 0) {//cout << "no palindrome" << endl;//getchar();return false;}else {//cout << "is palindrome" << endl;//getchar();return true;}            }};int main() {int a = 1000021;   //2147483648Solution solution;cout << solution.isPalindrome(a) << endl;getchar();}

0 0