需要解释递归Ç

编程入门 行业动态 更新时间:2024-10-24 04:51:52
本文介绍了需要解释递归Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一些帮助这个特殊的code。我真的不明白它以最充分的。可能有人走的时候,它给我解释一下?它的作用是作为一个单词,并打印出它的反面。我不明白,虽然递归部分,对于n = 1 for循环将只运行一次,递归将运行,直到它读取整个单词,它符合''标志,但它是如何打印出逆转字?

无效反向() {   焦炭℃;   scanf函数(%C,和C);   如果(C!='')       {        相反();       } 的printf(%C,C); } 诠释的main() { 诠释N,I; 的printf(字样\\ n此数=); scanf函数(%d个,&安培; N); 对于(i = 1; I< = N; ++ I)   {    相反();    的printf(\\ n);   } 的printf(程序的\\ NEND \\ n); 返回0; }

解决方案

注:的您的例子是在底部,首先你的标题问题:解释需要Ç递归的

递归的是一种编程技术,允许操作自称。结果一个简单的(但无意义的)例子是:

无效调用(无效);INT主要(无效){    呼叫();}无效调用(无效){    呼叫();}

的注意:的这只会去,直到堆栈确定了太多的呼声叠起来,并引起程序崩溃

一个更有意义的例子的(仅供说明)是写回文字符系列(A - Z):

无效回文(CHAR一){    炭B〔2〕;    sprintf的(B,%C,一);    的printf(B);    如果((A> = 65)及及(一个或下; 90))    {        回文(A + 1);    }    sprintf的(B,%C,一);    的printf(B);}

这最后一个例子实际上做两个重要的事情第一个例子中并没有这样做:结果 1)的已受控退出条件的如果((A> = 65)及及(A< = 90))的 2)的使用与后续调用之前调用的结果。

在您的例子的:程序工作的原因是,每个操作调用自身时,它被嵌套更深(每个呼叫一窝)插入部分:(这是所有递归程序真)

{    相反();  }

应用于类似的概念:的

{    //做一点事    {        //做一点事        {            //做一点事            // ...等尽可能多的递归是必要的,以满足退出条件        }    }}

......但它是如何打印出相反的词吗?的结果递归达到设定的限值也执行流下过去结束} 并击中了下一节,在那里继续放松本身只后,每次访问, 堆栈相反的顺序,的 的ç,直到每个嵌套层次的值已经解开:

} //退出条件被满足后,执行流程从这里....     的printf(%C,C); //顺序相反的C打印值。 } //这里,直到递归数耗尽。

在那个时候,如果通过使用调试器步骤,你会看到流量从上} 到较低} 执行的printf()每次

I need some help with this particular code. I do not really understand it to its fullest. Could someone take the time and explain it to me? What it does is take a word and prints out its reverse. I don't understand the recursion part though, for n=1 the for loop will only run once, and the recursion will run until it reads the whole word and it meets the ' ' mark, but then how does it print out the reversed word?

void reverse() { char c; scanf("%c", &c); if (c!=' ') { reverse(); } printf("%c", c); } int main() { int n, i; printf("\nThe number of the words="); scanf("%d", &n); for(i=1; i<=n; ++i) { reverse(); printf("\n"); } printf("\nEnd of the program.\n"); return 0; }

解决方案

NOTE Your example is at bottom, first your title question: Explaining needed C recursion

Recursion is a programming technique allowing operations to call themselves. A simple (but meaningless) example would be:

void call(void); int main(void) { call(); } void call(void) { call(); }

Note: This would simply go until the stack determines too many calls have stacked up, and crash the program.

A more meaningful example (for illustration) would be to write a palindrome character series (A - Z):

void Palindrome(char a) { char b[2]; sprintf(b, "%c", a); printf(b); if((a >= 65)&&(a < 90)) { Palindrome(a+1); } sprintf(b, "%c", a); printf(b); }

This last example actually does two important things the first example did not do: 1) has a controlled exit criteria if((a >= 65)&&(a <= 90)) 2) uses the results of prior calls with subsequent calls.

In your example: The reason the program works is that each time the operation calls itself, it is nesting deeper (one nest for each call) into the section: (this is true for all recursive programs)

{ reverse(); }

Similar in concept to:

{ //do something { //do something { //do something //... and so on for as many recursions are necessary to meet exit criteria } } }

"...but then how does it print out the reversed word?" Only after the recursion reaches the programmed limit does execution flow down past the closing } and hit the next section, where it continues to unwind itself, each time accessing, in reverse order of stack, the values of c until each nested level has been unwound:

} //after exit criteria is met, execution flow goes from here.... printf("%c", c); //prints values of c in reverse order. } //to here until number of recursions is exhausted.

at that time, if you step through using a debugger, you will see flow going from the upper } to the lower } executing printf() each time.

更多推荐

需要解释递归Ç

本文发布于:2023-11-30 20:30:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!