回 帖 发 新 帖 刷新版面

主题:[讨论]跪求一道数据结构的上机题~~

小弟是计算机的初学者~~现在有一道题~上机就是调试不出来~~有哪位高手可以教教我~`先谢谢了~~~[em5]
   假设称正读和反读都相同的字符序列为回文,例如:abba 和 abcba都是回文~,
  abcde和ababab,则不是回文,试写一个算法判别读如一个以@为结束符的字符是否是回文~~~
                         就是这个题~要上机可以运行出来的哦~~

回复列表 (共12个回复)

沙发

#include<iostream.h>
#define MAX 1000
int main()
{
    char a[MAX];
    cin>>a;
    int len=0;
    while(a[len]!='\0')len++;
    for(int i=0;i<=len/2;i++)
        if(a[i]!=a[len-i-1]){
            cout<<"not hui wen"<<endl;
            return 0;
        }
        else continue;
    cout<<"shi hui wen"<<endl;
    return 1;
}

板凳

不是滴~~我们要求用栈和队列来做~~~~   还可以教教我吗??

3 楼

入栈,出栈之后次序保持一致的就是啦!
我没猜错的话,你一定会说程序怎么实现啊!入栈,出栈的代码书上有的,参考一下吧

4 楼

不是滴~~我写出来啦~~但是上机实现不了~~~所以我想看看你们写的是怎么样滴~~

5 楼

贴出来看看.

6 楼

typedef char queue_entry;
#include<iostream.h>
#include<stack>
using namespace std;
enum error_code{success,underflow,overflow};
const int maxqueue=10;
class queue{
public:
    queue();
    bool empty()const;
    error_code append(const queue_entry &item);
    error_code serve();
    error_code retrieve(queue_entry &item)const;
protected:
    int count;
    int front,rear;
    queue_entry entry[maxqueue];
    
};
queue::queue()
{
    count=0;
    rear=maxqueue-1;
    front=0;

}
bool queue::empty()const
{
    return count==0;
}
error_code queue::append (const queue_entry &item)
{
    if(count>=maxqueue)return overflow;
    count++;
    rear=((rear+1)==maxqueue)?0:(rear+1);
    entry[rear]=item;
    return success;
}
error_code queue::serve()
{
    if(count<=0)return underflow;
    count--;
    front=((front+1)==maxqueue)?0:(front+1);
    return success;
}
error_code queue::retrieve (queue_entry &item)const
{
    if(count<=0)return underflow;
    item=entry[front];
    return success;
}



main()
{
    int i=0,j=0,k=0;
    queue strings;
    stack <char>string0;
    char c,b,d,e;
    cin.get(c);
    while(c!='@')
    {
        j++;
        strings.append (c); 
        cin.get(c);
    }
    i=j/2;

    while(k<i-1)
    {
        strings.retrieve (b);
        strings.serve ();
        string0.push(b); 
        k++;
    }
    
    strings.retrieve (b);
    if(j%2==0)
        strings.serve ();
    string0.push(b); 

    while(i>0)
    {
        strings.retrieve(d);
        strings.serve();
        e=string0.top();
        string0.pop();
        if(d!=e) {cout<<"不是回文"<<endl;return 0;}
        i--;
    }
    if(d==e) cout<<"是回文"<<endl;
    return 0;
}

7 楼

上面的程序中的queue队列是我自己定义的,当然你也可以有函数库中的,这个就随便你啦`

8 楼

谢谢你啦`~~~~但是我想应该没得那么复杂吧~~我门才刚刚学勒~`

9 楼

这个不复杂的,看着是比较那个,因为我是用自己定义的queue,如果用涵数库里的queue会很简单的````

10 楼

#include<queue>
#include<iostream.h>
#include<stack>
using namespace std;
main()
{
    int i=0,j=0,k=0;
    queue <char>strings;
    stack <char>string0;
    char c,b,d,e;
    cin.get(c);
    while(c!='@')
    {
        j++;
        strings.push (c); 
        cin.get(c);
    }
    i=j/2;

    while(k<i-1)
    {
        b=strings.front();
        strings.pop ();
        string0.push(b); 
        k++;
    }
    
    b=strings.front();
    if(j%2==0)
        strings.pop();
    string0.push(b); 

    while(i>0)
    {
        d=strings.front();
        strings.pop();
        e=string0.top();
        string0.pop();
        if(d!=e) {cout<<"不是回文"<<endl;return 0;}
        i--;
    }
    if(d==e) cout<<"是回文"<<endl;
    return 0;

我来回复

您尚未登录,请登录后再回复。点此登录或注册