天天看点

学越千山:C语言程序设计练习题(十一)

作者:LearningYard学苑
学越千山:C语言程序设计练习题(十一)

分享兴趣,传播快乐,增长见闻,留下美好!

亲爱的您,这里是LearningYard新学苑。

今天小编为大家带来

“学越千山:C语言程序设计练习题(十一)”,

欢迎您的访问。

Share interest, spread happiness, increase knowledge, and leave beautiful.

Dear, this is the LearingYard Academy!

Today, the editor brings the “

Learning to Cross a Thousand Mountains:

C Language Programming Exercise

Quesvtion (11)”.

Welcome to visit!

思维导图

Mind mapping

学越千山:C语言程序设计练习题(十一)

练习题

Exercise questions

学越千山:C语言程序设计练习题(十一)

#include <stdio.h>

int Fib(int n); //函数声明

//Function declaration

int count; //全局变量

//Global variable

int main()

{

int n,m,i;

printf("Input n:");

scanf("%d",&n);

for (i = 1; i <= n; i++)

{

count = 0; //计算下一项时初始化count

//Initialize count when calculating the next item

m = Fib(i);

printf("Fib(%d)=%d, count=%d\n",i,m,count);

}

return 0;

}

//函数功能:计算fib数列的第n项

//Function function: Calculate the nth term of the fib sequence

int Fib(int n)

{

count++;

if (n == 0) //基线情况

return 0; //Baseline situation

if(n == 1) //基线情况

return 1; //Baseline situation

else //一般情况

return Fib(n-2)+Fib(n-1);

//General situation

}

注意:

全局变量

(1)即不在任何语句块内定义的变量,在程序任何地方都可以访问它的值,因此全局变量能使函数之间的数据交流更加方便。

(2)如果我们滥用全局变量,也会让程序变得混乱,因为全局变量可能在任何一个函数被更改,这样的的调试和维护困难较大。

递归函数

(1)任何递归函数都应有至少一个基线情况,以保证一般情况递归到基线情况时可以顺利停止。

(2)一般来说能用递归函数解决的都可以用迭代法解决。递归函数相较于迭代程序要更直观、更清晰、可读性更好、更接近数学公式的表示,但迭代程序的执行效率更高。

Attention:

Global variable

(1) That is, a variable not defined in any statement block can access its value anywhere in the program, so Global variable can make data exchange between functions more convenient.

(2) If we abuse Global variable, the program will also become confused, because Global variable may be changed in any function, which is difficult to debug and maintain.

General recursive function

(1) Any General recursive function should have at least one baseline condition to ensure that the recursion can be stopped smoothly when it reaches the baseline condition in general.

(2) Generally speaking, anything that can be solved by General recursive function can be solved by Iterative method. Compared with iterative programs, General recursive function are more intuitive, clearer, more readable, and closer to the representation of Formula, but the execution efficiency of iterative programs is higher.

学越千山:C语言程序设计练习题(十一)

今天的分享就到这里了,

如果您对文章有独特的想法,

欢迎给我们留言。

让我们相约明天,

祝您今天过得开心快乐!

That's all for today's sharing.

If you have a unique idea about the article,

please leave us a message,

and let us meet tomorrow.

I wish you a nice day!

参考资料:百度翻译,C语言程序设计第4版(苏小红等)

本文由LearningYard新学苑整理并发出,如有侵权请后台留言沟通

继续阅读