1 solutions

  • 0
    @ 2024-12-10 22:16:30

    C :

    #include<stdio.h>
    #include<malloc.h>
    #include<stdlib.h>
    #define stack_size 50                                                                                            
    typedef char ElemType;
    
    typedef struct
    {  
    	 ElemType elem[stack_size];
         int top;
    }seqstack;
    
    void Initstack( seqstack * s)//初始化
    {
    	s->top =-1;
    }
    
    void push (seqstack *s ,ElemType x )//进栈
    {
    	if(s->top == stack_size -1)
    		return ;
    	s->top++;
    	s->elem[s->top]=x;
    	return ;
    }
    int pop(seqstack *s, ElemType * x)//出栈
    {
    	if(s->top ==-1)
    		return 0;
    	else 
    		{
    			*x=s->elem[s->top];
    			s->top--;
    			return 1;
    		}
    }
    
    
    void f(seqstack l)
    {
    	int i;
    	for(i=l.top;i>=0;i--)
    		printf("%c",l.elem[i]);
    
    }
    
    void main()
    {
    	int n,i;
    	char a ,b[stack_size];
     	seqstack h;
    	Initstack(&h);
    	scanf("%d",&n);
    	getchar();
    	for(i=0;i<n;i++)
    	{
    		scanf("%c",&b[i]);
    		push(&h,b[i]);
    	}
    
    	f(h);
    		
    	for(i=0;i<n;i++)
    	{
    		pop(&h,&a);
    		if(b[i]!=a)
    			break;
    	}
    	
    	printf("\n");
    	if(i==n)
    		printf("1");
    	else
    		printf("0");
    	printf("\n");
    
    
    }
    
    
    
    
    

    C++ :

    #include<iostream>
    #include<string.h>
    using namespace std;
    
    typedef struct
    {
    	char input[100];
    	int top;
    }stick ;
    
    
    
    int main()
    {
    	stick s;
    	char x[100];
    	int n,a;
    	cin>>n;
    	getchar();
    	for(int i=0;i<n;i++)
    		{
    			s.input[i]='\0';
    			x[i]='\0';
    		}
    	for(s.top=0;s.top<n;s.top++)
    		cin>>s.input[s.top];
    	for(s.top--;s.top!=-1;s.top--)
    		cout<<s.input[s.top];
    	for(int i=0;i<n;i++)
    	{
    		x[n-i-1]=s.input[i];
    	}
    	a=strcmp(x,s.input);
    	if(a)
             a=0;
        else
             a=1 ;
    	cout<<endl;
        cout<<a;
        getchar();
        getchar();
    	return 0;
    	
    }
    

    Pascal :

    var ch:char;
        s1,st1:string;
        i,n:longint;
    begin
      readln(n);
      for i:=1 to n do
      begin
        read(ch);
        s1:=s1+ch;
      end;
      for i:=n downto 1 do
      st1:=st1+s1[i];
      if st1=s1 then begin writeln(st1); writeln('1'); end
      else begin writeln(st1); writeln('0'); end;
    end.
    
    • 1

    Information

    ID
    528
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By