1 solutions

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

    C :

    #include <stdio.h>
    void main()
    {
      int i,a[100],k,count,key,n,j,b[100];
      scanf("%d",&n);
      while(n!=0)
      {
        k=key=0;
        for(i=0;i<n;i++)
          scanf("%d",&a[i]);
        for(i=0;i<n;i++)
        {
    		count=0;
          for(j=i+1;j<n;j++)
          {
            if(a[j]<a[i])
            {b[count]=a[j];
             count++;}
          }
          for(k=0;k<count-1;k++)
          {
            if(b[k]<b[k+1])
            key=1;
          }
        }
        if(key==0)
          printf("Yes\n");
    	else
    		printf("No\n");
         scanf("%d",&n);
      }
    }
    

    C++ :

    #include <stdio.h>
    
    int top;
    
    int Stack(int stack[], int a, int &pre)
    {
    	if (a <= pre)
    	{
    		if (stack[--top] != a)
    		{
    			//printf("%d %d\n", stack[top],a);
    			return 0;
    			
    		}
    		else return 1;
    	}
    	for (int i = pre + 1; i <= a; i++)
    	{
    		stack[top++] = i;
    	}
    	top--;
    	pre = a;
    	return 1;
    }
    
    int main(int argc, char *argv[])
    {
    	int n;
    	int a, stack[105];
    	while (scanf("%d", &n) != EOF)
    	{
    		if(n==0) break;
    		top = 1;
    		stack[0]=0;
    		int pre = 0;
    		int flag = 1;
    		for (int i = 0; i < n; i++)
    		{
    			scanf("%d", &a);
    			if (flag == 1)
    			{
    				flag = Stack(stack, a, pre);
    				/*for (int j=1; j<top; j++ )
    				{
    					printf("%d ",stack[j]);
    				}
    				printf("\n");*/
    			}
    		}
    
    		if (flag == 1)
    		{
    			printf("Yes\n");
    		}else
    			printf("No\n");
    	}
    	
    	return 0;
    }
    
    

    Pascal :

    label 1;
    var a,b,c:array[0..100]of integer;n,i,j,k:integer;
    function yes:boolean;
    var l:integer;
    begin
     yes:=true;
     for l:=1 to j-1 do
      if a[l]=c[i]then exit(false)
    end;
    begin
     readln(n);
     while n>0 do
      begin
       fillchar(a,sizeof(a),0);
       for i:=1 to n do
        begin
         b[i]:=i;
         read(c[i])
        end;
       a[1]:=1;
       k:=2;
       j:=1;
       for i:=1 to n do
        begin
         if yes then
          while a[j]<>c[i]do
           begin
            inc(j);
            a[j]:=b[k];
            inc(k)
           end else
          begin writeln('No');goto 1 end;
         a[j]:=0;
         dec(j)
        end;
       if (a[1]=0)and(a[2]=0)then writeln('Yes');
       1:
       readln(n)
      end
    end.
    

    Java :

    import java.util.Scanner;
    import java.util.Stack;
    
    public class Main {
    	public static void main(String[] args) {
    		int n = 0;
    		Scanner in = new Scanner(System.in);
    
    		while ((n = in.nextInt()) != 0) {
    			int a[] = new int[100];
    			for (int i = 0; i < n; i++)
    				a[i] = in.nextInt();
    			Stack s = new Stack();
    			int index = 0;
    			for (int i = 1; i <= n; i++) {
    				s.add(i);
    				while(!s.isEmpty()){
    					if (Integer.parseInt(s.lastElement().toString()) == a[index]) {
    						index++;
    						s.pop();
    					}else break;
    				}
    			}
    
    			if (s.isEmpty())
    				System.out.println("Yes");
    			else
    				System.out.println("No");
    
    		}
    	}
    }
    
    

    Python :

    import sys
    
    def stackQue(n,ls):
        if len(ls)!=n:
            return False     
        i=0
        while i<len(ls):
            tmpl=[c for c in ls[i:] if ls[i]>c]
            if not isSort(tmpl):
                return False
            i+=1
        return True
    
    def isSort(ls,reverse=False):
        if not reverse:
            i=0
            while i<len(ls)-1:
                if ls[i]<ls[i+1]:
                    return False
                i+=1
            return True
              
    i = 0          
    for line in sys.stdin:
        a = line.split()
        n = len(a)
        ls = map(lambda x:int(x),a)
        
        
        if n == 1 and ls[0]==1:
               i +=1
               if i%2 == 0:
                   print 'Yes'
    
        if 1< n <= 100:      
                re = stackQue(n,ls)
                if re is True:
                   print 'Yes'
                else:
                   print 'No'
              
              
    
    • 1

    Information

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