题解 | #火车进站#

火车进站

https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char list[6000][10];
int stack[10];
int train[10];
int kind = 0;
int n;
int cmp_char(const void *_a, const void *_b)
{
    char *a = (char *)_a;
    char *b = (char *)_b;
    return strcmp(a, b);
}
void DFS(int pos,int top,int index)
{
    if(pos == n)
    {
        kind ++ ;
        for(int i=0;i<n;i++)
        {
            list[kind][i] = list[0][i];
        }
        return;
    }
    if(index < n)
    {
        stack[++top] = train[index++];
        DFS(pos,top,index);
        top--;
        index--;
    }

    if(top>=0)
    {
        list[0][pos++] = stack[top--] + '0';
        DFS(pos,top,index);
        stack[++top] = list[0][--pos] - '0';
    }
}
int main()
{
  while (scanf("%d", &n) != EOF)
  {
    for (int i = 0; i < n; i++)
    {
      scanf("%d", &train[i]);
    }
    int top = -1;
    int index = 0;
    DFS(0, top, index);
    qsort(list + 1, kind, sizeof(list[1]), cmp_char);
 
    for (int i = 1; i <= kind; i++)
    {
      for (int j = 0; j < n; j++)
      {
        printf("%c ", list[i][j]);
      }
      printf("\n");
    }
  }
 
  return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 15:08
点赞 评论 收藏
分享
能干的三文鱼刷了10...:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
07-09 18:33
门头沟学院 Java
这么逆天每年都有人去???&nbsp;填多益网申就是大型的服从性测试
鲁大牛:辅导员在群里发了这个公司我就申了一下。网申居然要写当场开摄像头写两篇不少于三百字的作文。太逆天了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务