回文数字(Palindrome Number)

总时间限制:1000ms 内存限制: 65536kB

描述

给出一系列非负整数,判断是否是一个回文数。回文数指的是正着写和倒着写相等的数。

输入

一行,一个01字符串。

输出

若干行,每行是一个非负整数(不超过99999999)

样例输入

11
123
0
14277241
67945497

样例输出

YES
NO
YES
YES
NO


ps.这个题是若干行输入...

题目链接

ac代码

/*
@File     :   palidrome.cpp
@Time     :   2020/03/25 09:47:24
@Desc     :   回文数字(Palindrome Number)
*/
#include <iostream>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 20

using namespace std;
//栈
typedef struct 
{
    char data[MAX_LEN];
    int top;
}Stack;
//初始化
void InitStack(Stack *&stack);
//压栈
bool Push(Stack *&stack, const char num);
//弹栈
bool Pop(Stack *&stack);
//栈是否为空
bool Empty(const Stack *stack);
//获取栈顶
bool get_top(const Stack *stack, char &top);
//判断非负整数是否为回文数
bool JudgePalindrome(const string num);
int main(int argc, char const *argv[])
{
    char num[MAX_LEN];
    while (gets(num))                                //直接cin一个数据不能过
    {                                                //别问我为啥
         if (JudgePalindrome(num)) cout << "YES
";  //
         else cout << "NO
";
    }
    system("pause");
    return 0;
}
void InitStack(Stack *&stack)
{
    stack = (Stack*)malloc(sizeof(Stack));
    stack->top = -1;
}
bool Push(Stack *&stack, const char num)
{
    if (stack->top == MAX_LEN - 1) return false;
    stack->top++;
    stack->data[stack->top]  = num;
    return true;
}
bool Pop(Stack *&stack)
{
    if (Empty(stack)) return false;
    stack->top--;
    return true;
}
bool Empty(const Stack *stack)
{
    return (stack->top == -1);
}
bool get_top(const Stack *stack, char &top)
{
    if (Empty(stack)) return false;
    top = stack->data[stack->top];
    return true;
}
bool JudgePalindrome(const string num)
{
    Stack *stack;
    char top = 'n';
    int mid = num.size()/2 - 1;
    InitStack(stack);
    for (int i = 0; i <= mid; i++) Push(stack,num[i]);
    if (num.size()%2 == 0) {
        for (int i = mid + 1; i < num.size(); i++) {
            get_top(stack,top);
            if (top == num[i]) Pop(stack);
        }
    } else {
        for (int i = mid + 2; i < num.size(); i++) {
            get_top(stack,top);
            if (top == num[i]) Pop(stack);
        }
    }
   return Empty(stack);
}
原文地址:https://www.cnblogs.com/levarz/p/12781516.html