目 录CONTENT

文章目录

判断回文数(栈实现)

不争
2024-01-02 / 0 评论 / 0 点赞 / 2 阅读 / 806 字

判断回文数(栈实现)

#include<iostream>
using namespace std;
const int N = 1e+5;
int a[N],top = 0, stack[N];
int main(){

    int n;
    cout << "请输入n:";
    cin >> n;
    for (int i = 0; i < n ; i ++){
        scanf("%d",&a[i]);
    }

    int mid = n / 2 - 1;
    for (int i = 0 ; i <= mid ; i ++) stack[++top] = a[i];

    //while(top)
    //printf("%d ",stack[--top]);
    int next;
    if (n % 2 == 0){
        next = mid + 1;
    }else {
        next = mid + 2;
    }

    for (int i = next ; i < n  ; i ++) {
        if (a[i] != stack[top]) break;
        top --;
    }

    if(top==0) cout << "YES" << " " << top << endl;
    else cout << "NO" << " " << top << endl;
    return 0;
}
0

评论区