[问]使用递归函数打印出来的n! [英] [ques]use recursive function to print out n!
问题描述
这是我的C编程作业,
它要我输入10!并输出以下结果
10!
??
10 * 9!
? ?
9 * 8!
..................
2 * 1 !
??
1
我把它减去degress,所以使用两个for()循环来执行。 。
但是我花了两天的时间尝试并且无法弄清楚问题是什么。
有人能给我一个暗示吗?非常感谢你。
我用dec C ++编译完成C编程跟随
------------ -------------------------------------------------- --------------
#include< stdio.h>
#include< stdlib.h>
long fact(int num);
int main(void)
{
int i,j; / * counter * /
int ans = 1;
printf("输入n!:");
scanf(" ;%d",& i);
for(i = 10; i> = 1; i--)
{
for(j = 9; j> = 0; j--)
{
ans * = j;
printf (%d *%2d!=%d \ n,i,j,i * j);
}
printf(" ; \ n");
}
系统(暂停);
返回0;
}
/ *递归函数* /
长事实(int num)
{
if(num< ; = 1){
返回1;
}
else {
return(num * fact(num) -1));
}
}
-
?[1; 34m ¢〜¢w ^ w ^¢¢W¯¯¢W¯¯¢? ùá?[35mweiyu.csie.net?[34mùá?[30m ????????????????????????? [m
?[1; 34m¢x?[31m?y?[32maì?[33m?§?[34m?a¢xá?[?[36m keith?[33m ???????????????????????????????????????????????? m b [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ ¢áá?[37mμoaí?ó?[36m140.120.238.173?[30m ?????????????????? [m
嗨
这很容易。你不需要2个for循环。
动作!写道:这是我的C编程作业,
它要我输入10!并输出以下结果
10!
10 * 9!
a ??
9 * 8!
。 .................
2 * 1!
a ??
1
我带它减少通过degress,所以使用两个for()循环执行..
但是我花了两天时间尝试并且无法弄清楚问题是什么。
有人能给我一个暗示吗?非常感谢你。
当你遇到这样的问题时,请记住所有元素
不需要放在一个循环。
他们中的一些可能会形成一个系列而其他人必须完成
分开。
在你的情况下,前10个!可以单独完成。剩下的就形成一个
系列,作为
10!,(10-1)!
9!,(9-1)!< br $> b $ b .....
......
.........
1!,(1-1)!
既然你已经有了一个递归函数来计算一个名为fact()的数字的阶乘
,你就可以使用一个for循环来做
系列。
希望你能得到这个想法。如果没有,请随时与我联系。
问候
AK
所有者
程序员的总部
http://groups.google.com/group/ programhq
ke ** *****@weiyu.csie.net (行动!)写道:
这是我的C编程作业,
它想要我输入10!并输出以下结果
10!
10 * 9!
a ??
9 * 8!
。 .................
2 * 1!
a ??
1
我带它减少通过degress,所以使用两个for()循环来执行..
但是我花了两天时间尝试并且无法弄清楚问题是什么。
任何人都可以给我一个提示这个 ?非常感谢你。
[snippet]
基本上,你不需要for循环,如果您使用递归函数,如代码中定义的fact()
。因此,你想要的只是如下:
---从这里开始
#include< stdio.h>
#include< stdlib.h>
long fact(int num);
int main(void)
{
int i; / * counter * /
printf("输入n!:");
scanf("%d",& i);
事实(i);
}
/ *递归函数* /
长fact(int num)
{
if(num< = 1){
返回1;
}
else {
printf("%d!\ n | \ n%dx(%d-1)!\ n",num,num, num);
返回(num * fact(num-1));
}
}
---结束这里
-
Denis HG
Denis HG写道:ke*******@weiyu.csie.net (动作!)写道:
这是我的C编程作业,
它要我输入10!并输出以下结果
10!
10 * 9!
a ??
9 * 8!
。 .................
2 * 1!
a ??
1
我带它减少通过degress,所以使用两个for()循环来执行...
但我花了两天时间尝试并且无法弄清楚问题是什么。
任何人都可以给我一个提示有了这个 ?非常感谢你。
[snippet]
基本上,你不需要for和for。循环,如果您使用递归函数,如代码中定义的fact()
。因此,您想要的只是如下:
---从这里开始
#include< stdio.h>
#include< stdlib.h> ;
long fact(int num);
int main(void)
{
int i; / * counter * /
printf("输入n!:");
scanf("%d",& i);
事实(我);
}
/ *递归函数* /
long fact(int num)
{
if(num< = 1){
返回1;
}
其他{
printf("%d!\ n | \ n%dx(%d-1)!\ n",num, num,num);
return(num * fact(num-1));
}
}
---结束这里
-
Denis HG
我也在想同样的事情。我做了以下。但是,
scanf()仍然需要检查''时髦的输入字符''。我不知道
如何做到这一点。我想我可以考虑一下,但我也太懒了。
--------------- ---- ans.c ------------------------------------------- -------
#include< stdio.h>
#include< stdib.h>
int fact(int);
int fact(int num){
if(num(< = 1){
返回1;
}
else {
printf("%d!*%d!\ n",num, num-1);
}
}
int main(无效){
int i = 0;
printf("输入n!\ n");
scanf("%d!\ n", & i);
fact(i);
返回0;
}
-------------------------------------------------- ---------------------------
乍得
this is my C Programming homework,
It wants me to input 10! and output the follow result
10!
??
10 * 9!
??
9 * 8!
..................
2* 1!
??
1
I take it for decreasing by degress, so using two for() loop to execute..
but I spend two days to try and can not figure out what the problem is.
Could anybody show me a hint with this ? thank you so much.
I use the compiler with dec C++ to finish the C programming follow
----------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
long fact(int num);
int main(void)
{
int i,j; /* counter */
int ans = 1;
printf("Input the n!:");
scanf("%d", &i);
for(i=10;i>=1;i--)
{
for(j=9;j>=0;j--)
{
ans *= j;
printf("%d * %2d! = %d\n",i, j, i*j );
}
printf("\n");
}
system("pause");
return 0;
}
/* Recursive fonction */
long fact(int num)
{
if(num<=1){
return 1;
}
else{
return (num * fact(num-1));
}
}
--
[1;34m¢~¢w¢w¢w¢w¢? ùá [35mweiyu.csie.net [34mùá [30m??????????????????????????[m
[1;34m¢x[31m¤y[32maì[33m¤§[34m?a¢x ùá[36m keith [33m??????????????????????????????????????[m
[1;34m¢¢¢w¢w¢w¢w¢£ ùá [37mμoaí?ó [36m140.120.238.173 [30m????????????????????[m
Hi
This is quite easy. You don''t need 2 for-loops for that.
action! wrote:this is my C Programming homework,
It wants me to input 10! and output the follow result
10!
a??
10 * 9!
a??
9 * 8!
..................
2* 1!
a??
1
I take it for decreasing by degress, so using two for() loop to execute..
but I spend two days to try and can not figure out what the problem is.
Could anybody show me a hint with this ? thank you so much.
When you get questions like this, keep in mind that all the elements
need not be put in a single loop.
Some of them maybe formaing a series while others have to be done
seperately.
In your case, the first 10! can be done seperately. The rest forms a
series, as
10!, (10-1)!
9!, (9-1)!
.....
......
.........
1!,(1-1)!
Since you already have a recursive function to computer the factorial
of a number called fact(), you could use a single for loop to do the
series.
Hope you get the idea. If not, please feel free to contact me.
Regards
AK
Owner
Programmer''s HQ
http://groups.google.com/group/programhq
ke*******@weiyu.csie.net (action!) writes:
this is my C Programming homework,
It wants me to input 10! and output the follow result
10!
a??
10 * 9!
a??
9 * 8!
..................
2* 1!
a??
1
I take it for decreasing by degress, so using two for() loop to execute..
but I spend two days to try and can not figure out what the problem is.
Could anybody show me a hint with this ? thank you so much.
[snippet]
Basically, you don''t need a "for" loop if you employ a recursive function like the "fact()"
defined in your code. Therefore, what you want is simply as follows:
--- begin here
#include <stdio.h>
#include <stdlib.h>
long fact(int num);
int main(void)
{
int i; /* counter */
printf("Input the n!:");
scanf("%d", &i);
fact(i);
}
/* Recursive fonction */
long fact(int num)
{
if(num<=1){
return 1;
}
else{
printf("%d!\n|\n%d x (%d-1)!\n", num, num, num);
return (num * fact(num-1));
}
}
--- end here
--
Denis H. G.
Denis H. G. wrote:ke*******@weiyu.csie.net (action!) writes:this is my C Programming homework,
It wants me to input 10! and output the follow result
10!
a??
10 * 9!
a??
9 * 8!
..................
2* 1!
a??
1
I take it for decreasing by degress, so using two for() loop to execute...
but I spend two days to try and can not figure out what the problem is.
Could anybody show me a hint with this ? thank you so much.
[snippet]
Basically, you don''t need a "for" loop if you employ a recursive functionlike the "fact()"
defined in your code. Therefore, what you want is simply as follows:
--- begin here
#include <stdio.h>
#include <stdlib.h>
long fact(int num);
int main(void)
{
int i; /* counter */
printf("Input the n!:");
scanf("%d", &i);
fact(i);
}
/* Recursive fonction */
long fact(int num)
{
if(num<=1){
return 1;
}
else{
printf("%d!\n|\n%d x (%d-1)!\n", num, num, num);
return (num * fact(num-1));
}
}
--- end here
--
Denis H. G.
I was also thinking the same thing. I did the following. HOWEVER,
scanf() still has to checked for ''funky input chars''. I had no idea how
to do this. I suppose I could have thought about it, but I was too
lazy.
-------------------ans.c--------------------------------------------------
#include <stdio.h>
#include <stdib.h>
int fact(int);
int fact(int num) {
if(num(<=1){
return 1;
}
else {
printf("%d! * %d! \n",num,num-1);
}
}
int main(void) {
int i = 0;
printf("Input the n! \n");
scanf("%d! \n", &i);
fact(i);
return 0;
}
-----------------------------------------------------------------------------
Chad
这篇关于[问]使用递归函数打印出来的n!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!