首页 > 其他 > 详细

P 1008 说反话

时间:2019-10-06 13:01:07      阅读:89      评论:0      收藏:0      [点我收藏+]

  这道题我记得谷歌 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不易,诸君共勉!

P 1008 说反话

原文:https://www.cnblogs.com/daker-code/p/11625974.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!