递归因子函数。 [英] recursive factorial function.

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

问题描述




考虑(factorial.cpp):

#include< iostream>

using namespace std;


双R = 3.2; / *未使用,但R是静态的,因为它是一个全局的
变量(文件范围)* /


int F(int n); / *原型* /


int main()

{

int number;

cout << 输入一个整数,其中找到的阶乘:" ;;

cin>>数字;

cout<< 让我们看看什么因子(<<<<<<<")给了我们。它给了

us: << F(数量)<< endl;

}


int F(int i)/ *自动存储变量 - >在堆栈中分配(在执行此函数时仅存在
)* /

{

static int count = 0; / *计数另一方面是静态的(明确地)

- >在整个程序运行期间存在* /


++ count; / *增量计数器* /


if(i == 0)

{

cout<< Count = <<计数<< endl<<结束;

返回1;

}


其他

返回i * F( - i);

}


我想知道为什么它给我输出0(计数打印后)

到屏幕)...另外:我不知道如何打印出正确的

阶乘值...

祝你好运/ Med venlig hilsen

Martin J?rgensen


-

------------------ -------------------------------------------------- -------

Martin J?rgensen的主页 - http: //www.martinjoergensen.dk

解决方案

" Martin J?rgensen"写道:

考虑(factorial.cpp):

#include< iostream>
使用命名空间std;

双R = 3.2; / *未使用,但R是静态的,因为它是一个全局变量
(文件范围)* /

int F(int n); / *原型* /

int main()
{/ / int int number;
cout<< 输入一个整数,以查找阶乘:" ;;
cin>>数字;
cout<< 让我们看看什么因子(<<<<<<<")给了我们。它给了我们:
<< F(数量)<< endl;
}
int F(int i)/ *自动存储变量 - >在堆栈中分配(在执行此函数时仅存在)* /
{
static int count = 0; / *计数另一方面是静态的(明确地) - >
在整个程序运行期间存在* /

++ count; / *增量计数器* /

if(i == 0)
{
cout<< Count = <<计数<< endl<<结束;
返回1;
}

返回i * F( - i);
}

我想知道为什么它给我输出0(在计数打印到屏幕后)......还有:我不知道如何打印出正确的因子值。 ..




你让它变得多么复杂。不需要静态的

变量,例如count。我建议你从头开始用

介意。


Martin J?rgensen skrev:

你好,

考虑(factorial.cpp):

#include< iostream>
使用命名空间std;

double R = 3.2 ; / *未使用,但R是静态的,因为它是一个全局的变量(文件范围)* /

int F(int n); / *原型* /


< snip>
int F(int i)/ *自动存储变量 - >在堆栈中分配(在执行此函数时仅存在)* /
{
static int count = 0; / *指望另一方面是静态的
(明确地) - >在整个程序运行期间存在* /

++ count; / *增量计数器* /

if(i == 0)
{
cout<< Count = <<计数<< endl<< endl;
返回1;
}

其他
返回i * F( - i);


返回i * F(i - 1);


为什么?在第二次递归''我'将等于0,并且

声明


返回0 * F(0);


将返回0.

}



http://www.parashift.com/c++-faq-lit...html#faq-39.15

TB @ SWEDEN


" Martin J?rgensen" <未********* @ spam.jay.net>在留言中写道

news:b9 ************ @ news.tdc.dk ...


... 。

return i * F( - i);
我想知道为什么它给我0作为输出(计数被打印到屏幕后)...




表达式i * F( - i)使用并修改两个

序列点之间的i值。因此,它的行为是未定义的,并且在此程序中找到* any *其他错误之前没有任何意义

,直到此错误为

更正。


Hi,

Consider (factorial.cpp):
#include <iostream>
using namespace std;

double R=3.2; /* not used, but R is static because it is a global
variable (file scope) */

int F(int n); /* prototype */

int main()
{
int number;
cout << "Enter an integer of which to find the factorial of: ";
cin >> number;
cout << "Let''s see what factorial(" << number << ") gives us. It gives
us: " << F(number) << endl;
}

int F(int i) /* automatic storage variable -> allocated in a stack (only
exists during execution of this function) */
{
static int count = 0; /* count on the other hand is static (explicitly)
-> exists during the whole program run */

++count; /* increment counter */

if (i==0)
{
cout << "Count = " << count << endl << endl;
return 1;
}

else
return i*F(--i);
}

I''m wondering why it gives me 0 as output (after count has been printed
to the screen)... Also: I''m not sure how to print out the correct
factorial value...
Best regards / Med venlig hilsen
Martin J?rgensen

--
---------------------------------------------------------------------------
Home of Martin J?rgensen -
http://www.martinjoergensen.dk

解决方案

"Martin J?rgensen" writes:

Consider (factorial.cpp):
#include <iostream>
using namespace std;

double R=3.2; /* not used, but R is static because it is a global variable
(file scope) */

int F(int n); /* prototype */

int main()
{
int number;
cout << "Enter an integer of which to find the factorial of: ";
cin >> number;
cout << "Let''s see what factorial(" << number << ") gives us. It gives us:
" << F(number) << endl;
}

int F(int i) /* automatic storage variable -> allocated in a stack (only
exists during execution of this function) */
{
static int count = 0; /* count on the other hand is static (explicitly) ->
exists during the whole program run */

++count; /* increment counter */

if (i==0)
{
cout << "Count = " << count << endl << endl;
return 1;
}

else
return i*F(--i);
}

I''m wondering why it gives me 0 as output (after count has been printed to
the screen)... Also: I''m not sure how to print out the correct factorial
value...



You''re making it unnecessarily complicated. There is no need for a static
variable such as count. I suggest you start from scratch with that tidbit in
mind.


Martin J?rgensen skrev:

Hi,

Consider (factorial.cpp):
#include <iostream>
using namespace std;

double R=3.2; /* not used, but R is static because it is a global
variable (file scope) */

int F(int n); /* prototype */

<snip>

int F(int i) /* automatic storage variable -> allocated in a stack (only
exists during execution of this function) */
{
static int count = 0; /* count on the other hand is static
(explicitly) -> exists during the whole program run */

++count; /* increment counter */

if (i==0)
{
cout << "Count = " << count << endl << endl;
return 1;
}

else
return i*F(--i);
return i * F(i - 1);

Why? On the second last recursion ''i'' will equal 0, and
the statement

return 0 * F(0);

will return 0.
}



http://www.parashift.com/c++-faq-lit...html#faq-39.15

--
TB @ SWEDEN


"Martin J?rgensen" <un*********@spam.jay.net> wrote in message
news:b9************@news.tdc.dk...

....

return i*F(--i); I''m wondering why it gives me 0 as output (after count has been printed to
the screen)...



The expression i*F(--i) uses and modifies the value of i between two
sequence points. Therefore its behavior is undefined, and there is no point
in trying to find *any* other bugs in this program until this error is
corrected.


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

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