像计算机一样思考的策略
我在考试中遇到一些问题,需要推断出以下代码的输出:
01 int foo(int a) {
02 print 'F';
03 if (a <= 1) return 1;
04 return bar(a, foo(a-1));
05 }
06
07 int bar(int x, int y) {
08 print 'B';
09 if (x > y) return baz(x, y);
10 return baz(y, x);
11 }
12
13 int baz(int x, int y) {
14 print 'Z'
15 if (y == 0) return 0;
16 return baz(x, y-1) + x;
17 }
18
19 void main() {
20 foo(3);
21 }
我的问题是哪种策略最适合解决此类问题?当然我不被允许使用电脑 PS你可以使用c++中的急切求值或正常顺序求值(输出当然会有所不同,但我只对策略感兴趣),我尝试使用堆栈来解决它,每次编写我调用的函数,但无论如何这很复杂 预先感谢您的帮助
I have some question from the exam in which I need to deduce the output of the following code:
01 int foo(int a) {
02 print 'F';
03 if (a <= 1) return 1;
04 return bar(a, foo(a-1));
05 }
06
07 int bar(int x, int y) {
08 print 'B';
09 if (x > y) return baz(x, y);
10 return baz(y, x);
11 }
12
13 int baz(int x, int y) {
14 print 'Z'
15 if (y == 0) return 0;
16 return baz(x, y-1) + x;
17 }
18
19 void main() {
20 foo(3);
21 }
my question is what tactic will be the best to solve this kind of the questions? I'm not allowed to use PC of course
P.S. You can use eager evaluation as in c++ or normal order evaluation(output will be different of course, but I'm interested in tactics only), I tried to solve it using stack, every time write the function which I call, but anyway it is complicated
thanks in advance for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用“自下而上”的尝试:
baz 是被调用的函数,但不调用其他函数(除了它本身)。它精确地输出“Z”
y + 1
次,返回码为x*y
(每次调用后添加x
)。bar
是“下一个更高”函数,它输出 'B' 一次,并使用其较低的参数作为第二个参数调用baz
- 返回代码为x* y 也是。
foo
是“顶级”函数(紧随main
之后),也是最复杂的函数。它不仅输出一次“F”,而且输出a
次(因为末尾的foo(a-1)
在bar< /code> 调用将
a
与foo(a-1)
相乘,从而将a-1
相乘。 code> 和foo(a-2)
等等,直到foo(1)
被计算并返回 1。所以返回码是a * (a -1) * ... 2 * 1
,所以a!
,这不是一个完整的分析,我们不知道字符将以什么顺序输出,但它是所发生情况的粗略计划 - 正如您和其他人在评论中指出的那样,这就是您想要的 - 策略而不是完整的答案。
I would use a "bottom-to-top" attempt:
baz
is the function that is called, but doesn't call other functions (except itself). It outputs 'Z' exactlyy + 1
times, the return code isx*y
(you addx
after each call).bar
is the "next higher" function, it outputs 'B' once and callsbaz
with its lower argument as the second parameter - the return code isx*y
, too.foo
is the "top" function (right aftermain
) and its the most complicated function. It outputs 'F', not only once, buta
times (because of thefoo(a-1)
at the end that is evaluated before thebar
call. Thebar
call multipliesa
andfoo(a-1)
, which will multiplya-1
andfoo(a-2)
and so on, untilfoo(1)
is evaluated and returns 1. So the return code isa * (a-1) * ... 2 * 1
, soa!
.This is not a complete analysis, f.e. we don't know in which order the characters will be output, but it is a rough scheme of what happens - and as you and other people in the comments pointed out, this is what you want - tactics instead of a complete answer.
我可能要做的就是从页面左上角的 main() 函数开始,写下执行的第一行,跟踪局部变量等,然后编写下一行其下的线等等。
但是,当调用函数时,还要向右移动一列,首先写下函数的名称和该调用的输入参数的实际值,然后继续处理该函数中的行。
从函数返回时,向左移动并将返回值写入两列之间。
另外,为“标准输出”保留一个单独的区域,所有打印的文本都存放在那里。
这些步骤应该可以帮助您解决大多数“像计算机一样思考”的问题。
What I'd probably do is to start with the
main()
function at the top left corner of the page, write down the first line executed, keeping track of local variables etc., then write the next line under it and so on.But when a function is called, also move right by one column, writing down the function's name and the actual value of the input arguments for that invocation first and then proceding with the lines in that function.
When you return from the function, move left and write the return value between the two columns.
Also, keep a separate area for the "standard output", where all the printed text goes.
These steps should take you through most of "think like a computer" problems.