像计算机一样思考的策略

发布于 2024-10-12 07:55:33 字数 520 浏览 10 评论 0原文

我在考试中遇到一些问题,需要推断出以下代码的输出:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玉环 2024-10-19 07:55:33

我会使用“自下而上”的尝试:

baz 是被调用的函数,但不调用其他函数(除了它本身)。它精确地输出“Z”y + 1 次,返回码为 x*y(每次调用后添加 x)。

bar 是“下一个更高”函数,它输出 'B' 一次,并使用其较低的参数作为第二个参数调用 baz - 返回代码为 x* y 也是。

foo 是“顶级”函数(紧随 main 之后),也是最复杂的函数。它不仅输出一次“F”,而且输出 a 次(因为末尾的 foo(a-1)bar< /code> 调用将 afoo(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' exactly y + 1 times, the return code is x*y (you add x after each call).

bar is the "next higher" function, it outputs 'B' once and calls baz with its lower argument as the second parameter - the return code is x*y, too.

foo is the "top" function (right after main) and its the most complicated function. It outputs 'F', not only once, but a times (because of the foo(a-1) at the end that is evaluated before the bar call. The bar call multiplies a and foo(a-1), which will multiply a-1 and foo(a-2) and so on, until foo(1) is evaluated and returns 1. So the return code is a * (a-1) * ... 2 * 1, so a!.

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.

鸢与 2024-10-19 07:55:33

我可能要做的就是从页面左上角的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文