递归 [英] Recursively

查看:57
本文介绍了递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




需要对以下示例进行一些澄清wrt

递归。

void f(char *);

main()

{

f(" 123");

}

void f(char a [])

{

if(a [1] ==''\''')

返回;

f(a + 1);

f(a + 1);

printf("%c",a [1 ]);

}


此程序的输出已标为332.

怎么可能?


它实际上应该在没有打印的情况下返回。

如何输出332作为输出?


Thx in advans,

Karthik Balaguru

解决方案

karthikbalaguru写道:





需要对以下示例进行一些澄清wrt

递归。


void f(char *);

main()

{

f(" 123");

}

void f(char a [])

{

如果(a [1] ==''\''')

返回;

f(a + 1);

f(a + 1);

printf("%c",a [1]);

}

这个程序的输出已经被声明为332.

怎么可能?


它实际上应该返回而不打印任何东西。

怎样才能得到332作为输出?



为什么?当f用3调用时会发生什么?


-

Ian Collins。


< blockquote> karthikbalaguru写道:


void f(char *);

main()

{

f(" 123");

}

void f(char a [])

{

if(a [1] ==''\''')

返回;

f(a + 1);

f(a + 1);

printf("%c",a [1]);

}


此程序的输出已表示为332.

怎么可能?



这就是它的工作方式。


它应该实际返回而不打印任何东西。



怎么样?如果你只考虑最外面的电话,/ / / b $ b返回印刷品[1] - 123[1]不是'\\'0'',

所以它会做最后的'printf`。


怎么能得到''332'作为输出?



让我们看看代码。


if(a [1] ==' '\0'')

返回;

f(a + 1);

f(a + 1);

printf("%c",a [1]);



我实际上会把它重写为


if(a [1]!=''\ 0' ')

{

f(a + 1);

f(a + 1);

putchar(a [1]);

}


[其实我会把第一位写成`if(a [1])`;我没有担心C'的条件测试。


测试是这个字符串中有第二个字符吗?

如果是这样,再次打印字符串的其余部分。然后打印这个

的第二个字符。没问题。


[编写代码的人要么就是用

学生的关于索引的想法来玩游戏,要么就是真的不懂C'

索引自己。或者其他什么。]


[1]除了没有最终换行符,所以也许它不会打印

任何东西 - 很容易修复在`main`的末尾添加`putchar(''\ n'');`[和

`返回0;`]。


-

原始,递归,刺猬

引用设施包括缺乏原创思想。 /华丽之夜/


文章< 11 ********************** @ a39g2000hsc.googlegroups .com>,

karthikbalaguru< ka *************** @ gmail.comwrote:


> void f(char *);
main()

f(" 123");
} void f(char a [])
{

if(a [1] ==''\ 0'')

return;

f(a + 1);

f(a + 1);

printf("%c",a [1]);
}

此程序的输出已声明为332.
如何可能?



f(" 123")被调用。它调用f(23)两次,然后打印2。

f(" 23")两次调用f(3),然后打印3。 br />
f(" 3")没有打印任何东西。


所以3打印两次,然后打印2。


你是否真的看到任何东西是系统相关的,因为最后没有打印

换行符。 />

- Richard

-

应考虑最多32个字符的需要
一些字母表中的
" - 1963年的X3.4。


Hi,

Need some clarification regarding the following example w.r.t
recursive .
void f(char *) ;
main()
{
f("123");
}
void f(char a[])
{
if(a[1]==''\0'')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);
}

The output for this program has been stated as 332.
How is it possible ?

It should actually return without printing anything.
How can one get ''332'' as output ?

Thx in advans,
Karthik Balaguru

解决方案

karthikbalaguru wrote:

Hi,

Need some clarification regarding the following example w.r.t
recursive .
void f(char *) ;
main()
{
f("123");
}
void f(char a[])
{
if(a[1]==''\0'')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);
}

The output for this program has been stated as 332.
How is it possible ?

It should actually return without printing anything.
How can one get ''332'' as output ?

Why? What happens when f is called with "3"?

--
Ian Collins.


karthikbalaguru wrote:

void f(char *) ;
main()
{
f("123");
}
void f(char a[])
{
if(a[1]==''\0'')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);
}

The output for this program has been stated as 332.
How is it possible ?

That''s the way it works.

It should actually return without printing anything.

How? If you consider only the outermost call, /that/
returns printing something [1] -- "123"[1] isn''t ''\0'',
so it will do the final `printf`.

How can one get ''332'' as output ?

Let''s look at the code.

if(a[1]==''\0'')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);

I''d actually rewrite it as

if (a[1] != ''\0'')
{
f( a + 1 );
f( a + 1 );
putchar( a[1] );
}

[Actually I''d write that first bit as `if (a[1])`; I have
no fears of C''s conditional tests.]

The test is "is there are second character in this string?"
If so, print the rest of the string, again. Then print this
second character. No problem.

[Whoever wrote the code was either playing head-games with the
student''s ideas about indexing or didn''t really understand C''s
indexing themselves. Or something.]

[1] Except there''s no final newline, so perhaps it won''t print
anything -- easily fixed by adding `putchar( ''\n'' );` [and
`return 0;`] at the end of `main`.

--
Primitive, Recursive, Hedgehog
"A facility for quotation covers the absence of original thought." /Gaudy Night/


In article <11**********************@a39g2000hsc.googlegroups .com>,
karthikbalaguru <ka***************@gmail.comwrote:

>void f(char *) ;
main()
{
f("123");
}
void f(char a[])
{
if(a[1]==''\0'')
return;
f(a+1);
f(a+1);
printf("%c", a[1]);
}

The output for this program has been stated as 332.
How is it possible ?

f("123") is called. It calls f("23") twice, then prints "2".
f("23") calls f("3") twice, then prints "3".
f("3") doesn''t print anything.

So "3" gets printed twice, then "2".

Whether you actually see anything is system dependent, because no
linefeed is printed at the end.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.


这篇关于递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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