zoukankan      html  css  js  c++  java
  • Rails problem

    总是wa~

    #include <stdio.h>
    int main()
    {
        int n, i, j, k, atop, cmd[20];
        char a[10], b[10];
        while(scanf("%d %s %s", &n, a, b) != EOF){
    
            for(i = 0, j = 0, k = 0; i < n; i++){
    
                    atop = i + 1;
                    cmd[k++] = 1;
                    while(atop > 0 && a[atop - 1] == b[j]){
    
                            
                            cmd[k++] = 0;
                            j++;
                            atop--;
    
                    }
    
            }
            if(j == n)
            {
    
                    printf("Yes.
    ");
                    for(i = 0; i < k; i++){
    
                            if(cmd[i] == 1)
                                printf("in
    ");
                            else
                                printf("out
    ");
    
                    }
                    printf("FINISH
    ");
    
            }
            else
            {
                    
                    printf("No.
    FINISH
    ");
    
            }
    
        }
        return 0;
    }
    View Code

    经过修改,发现问题出现在栈的使用,调试了这组数据:5 67543 74536,才发现自己没有清栈。

     1 #include <stdio.h>
     2 int main()
     3 {
     4     int n, i, j, k, top, cmd[20];
     5     char a[10], b[10], c[10];
     6     while(scanf("%d %s %s", &n, a, b) != EOF){
     7         
     8         top = 1;
     9         for(i = 0, j = 0, k = 0; i < n; i++){
    10 
    11                 c[top++] = a[i];/*入栈*/
    12                 cmd[k++] = 1;                   
    13                 while(c[top-1] == b[j]){
    14                         
    15                         c[top-1] = 0;/*出栈*/
    16                         top--;
    17                         cmd[k++] = 0;
    18                         j++; 
    19                         
    20                 }
    21 
    22         }
    23         if(j == n)
    24         {
    25 
    26                 printf("Yes.
    ");
    27                 for(i = 0; i < k; i++){
    28 
    29                         if(cmd[i] == 1)
    30                             printf("in
    ");
    31                         else
    32                             printf("out
    ");
    33 
    34                 }
    35                 printf("FINISH
    ");
    36 
    37         }
    38         else
    39         {
    40                 
    41                 printf("No.
    FINISH
    ");
    42 
    43         }
    44 
    45     }
    46     return 0;
    47 }
    View Code

    解决此问题后,写下自己的用栈心得:

    入栈,先赋值再上移栈顶;

    出栈,清零再下移栈顶。

    注意:一定要保持栈的数目不变,这样才是栈的使用规则

  • 相关阅读:
    class类文件具有错误的版本52.0,应为50.0
    git learn.
    git diff 命令用法
    vlan
    bridge
    Packet flow in l2(receive and transmit)
    /proc/uptime详解
    linux 内核数据结构之红黑树.
    linux 内核数据结构之 avl树.
    python学习.
  • 原文地址:https://www.cnblogs.com/the-one/p/3252695.html
Copyright © 2011-2022 走看看