P 1008 说反话

转跳点:🐏  

这道题我记得谷歌 2012 年校招,百度201年校招好向出过。所以我想,了一个及其奇葩的代码。(模拟了栈的理念)代码有些奇葩。

 

  我利用了scanf函数遇到“ ”停止的特性模拟了栈的push,用printf遇到'\0'结束模拟了pop,设定了一个栈顶指针指向当前栈顶元素。因为栈的先进后出的特性,加上%s输出的特性,就完成了对单词顺序的反转

代码实现如下:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define MAXSIZE 100
 4 
 5 typedef struct
 6 {
 7     char Str[MAXSIZE / 4];
 8 } Stack;
 9 
10 int main()
11 {
12     int bottom = -1;
13     Stack str[MAXSIZE];
14 
15     while (scanf("%s", &str[++bottom]) != EOF && str[bottom].Str[0] != '#')
16         ;
17     while (bottom != 0)
18         printf("%s%s", str[bottom].Str, 0 == --bottom ? "" : " ");
19 
20     return 0;
21 }
结构体数组

   这个是用二维数组实现的:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define MAXSIZE 100
 4 
 5 int main()
 6 {
 7     int bottom = -1;
 8     char str[MAXSIZE][MAXSIZE / 4];
 9 
10     while (scanf("%s", &str[++bottom]) != EOF && str[bottom][0] != '\n')
11         ;
12 
13     while (bottom != 0)
14         printf("%s%s", str[bottom], 0 == --bottom ? "" : " ");
15 
16     return 0;
17 }
二维数组

 

   PAT不易,诸君共勉!

posted @ 2019-10-06 10:26  秦_殇  阅读(150)  评论(0编辑  收藏  举报