我如何阅读本程序并获得答案? [英] How Do I Read This Program And Get The Answer?

查看:60
本文介绍了我如何阅读本程序并获得答案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   iostream  >  
#include < iomanip < span class =code-keyword>>
使用 namespace std;

int fn( int a){
cout< < a<< - ;
if ((a == 0 )||(a == 1 ))
return a;
return fn(a - 1 )+ fn(a - 2 );
}

int main(){
int a = fn( 4 );
cout<< a<< ENDL;
return 0 ;
}





解决这个问题有哪些步骤?

解决方案

1)打开Visual Studio。

2)将代码复制并粘贴到一个新项目中。

3)运行应用程序。



或者,浏览代码并计算出每次基于行开头的值执行行时会发生什么。



这是你的作业,而不是我们的作业:所以你应该这样做,而不是我们。


你必须把自己当作调试器

让我们一起完成第一步,从 main 开始,当然,我们有:

  int  a = fn( 4 ); 



为了将正确的值分配给 a ,我们必须评估 fn(4)



所以,让我们进入 fn(4 )

  • 我们可以安全地忽略 cout 行。
  • if 条件的计算结果为false,因为 4 既不是 0 也不是 1
  • 最终我们到达了这一行

     return fn(3)+ fn(2); 



    这告诉我们我们必须再次(再次)进入 fn ,不同参数值,以便了解我们的最终结果。





我想这足以让你弄明白如何完成作业

#include <iostream>
#include <iomanip>
using namespace std;

int fn(int a) {
	cout << a << "-";
	if ((a == 0) || (a == 1))
		return a;
	return fn(a - 1) + fn(a - 2);
}

int main() {
	int a = fn(4);
	cout << a << endl;
	return 0;
}



What are the steps for solving this question?

解决方案

1) Open Visual studio.
2) Copy and paste the code into a new project.
3) Run the application.

Alternatively, walk through the code and work out what happens each time you execute a line based on the values the line started with.

This is your homework, not ours: so it's up to you to do this, not us.


You have to use yourself as a debugger.
Let's make together just the first steps, starting from main, of course, we have:

int a = fn(4);


In order to assign the correct value to a we have to evaluate fn(4).

So, let's step into fn(4):

  • We can safely ignore the cout line.
  • The if condition evaluates to false, since 4 is neither 0 nor 1.
  • Eventually we reach the line

    return fn(3) + fn(2);


    This tell us we have to step again (and again) into fn, with different argument values, in order to know our final result.



I suppose it is enough to make you figure out how to complete the homework.


这篇关于我如何阅读本程序并获得答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆