zoukankan      html  css  js  c++  java
  • 回文数字(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);
    }
    
  • 相关阅读:
    C++实现网络寻路
    java实现生日相同概率
    java实现生日相同概率
    Mysql 锁表 for update (引擎/事务)
    mysql(for update)悲观锁总结与实践
    Select For update语句浅析
    Mysql查询语句使用select.. for update导致的数据库死锁分析
    数据库中Select For update语句的解析
    【转载】支付宝运营架构中柔性事务指的是什么?
    互联网支付系统整体架构详解
  • 原文地址:https://www.cnblogs.com/levarz/p/12781516.html
Copyright © 2011-2022 走看看