我们如何使用C中的递归方法打印1 + 11 + 111 + ........系列的总和 [英] How can we print the sum of series 1+ 11 +111+........ upto N terms using recursion in C
问题描述
我一直试图使用递归来打印这个系列的总和,但最终只返回它的第n项。我知道如何使用迭代打印系列的总和,但使用递归打印它的总和已经太困难了。
经过很多思考,我来到了这就是最终的代码。
#include< stdio.h>
int sumseries(int);
int main()
{
int n;
printf(输入数字:);
scanf(%d,& n);
printf(这个系列的总和是%d,sumseries(n));
}
int sumseries(int n)
{
int i,sum = 0;
if(n == 1)
return 1;
for(i = 0; i {
sum = sum * 10 + 1;
}
return(sum + sumseries(n-1));
}
你可以在不使用for循环的情况下帮助我吗?
感谢您提供的答案!
所以,如果你这样做那么你会怎么做呢?
好的,你可能会写下每一项:
1 + 11 + 111 + 1111 + 11111 + 111111
然后你开始拿钱: 1 + 11 = 12
, 12 + 111 = 123
, 123 + 1111 = 1234
等等。
当然,你也可以把这些数字倒过来: 111111 + 11111 = 122222
, 122222 + 1111 = 123333
等。
为了用递归来攻击它,你需要仔细计划函数返回什么以及它的参数是什么。
在这个问题中,你需要计算两件事情:实际的系列项(1,11,111,1111等)和总和这些条款。你的函数的名字很明显,你打算 sumseries
返回系列的总和。
现在,你似乎已经有了一种方法来完成当前任期的下一个任务(乘以10并加1),但这对递归没有帮助。
然而,如果您有一种方法可以将当前术语添加到其余部分的总和中,那么您将会朝着递归解决方案的方向前进。
那么如果你的函数有两个参数:一个 然后你可以在你的函数的一部分中做到这一点: 事实上,这是它的核心: 然后在 I have tried continuously to print the sum of this series using recursion but ended up to return its nth term only. I know how to print the sum of series using iteration but it has been too hard for me to print its sum using recursion. After a lot of thinking, I came upto this as the final code. Can you help me with this without using the for loop? Thanks in advance for the answer! So, if you were doing this by hand, how would you do it? Well, by hand you'd probably write each term down: And then you'd start taking sums: Of course, you could also take the sums going backwards: In order to attack this with recursion, you'll need to carefully plan out what the function is returning and what its arguments are. You have, in this problem, two things you need to calculate: the actual series terms (1, 11, 111, 1111, etc) and the sum of those terms. As is clear by the name of your function, you intend Now, you seem to have in hand already a way to make the next term from the current term (multiply by 10 and add 1), but that isn't helping with the recursion yet. If you had a way to say "current term" added to "sum of the rest of the stuff", though, you'd be on your way towards a recursive solution. So what if your function took two parameters: an Then you could do this in part of your function: And in fact, that's the core of it: And then change the call in 这篇关于我们如何使用C中的递归方法打印1 + 11 + 111 + ........系列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! n
,表示要写多少条件,一个 current_term
给出当前期限的变量?
next_term = 10 * current_term + 1
return current_term + sumseries(n-1,next_term);
int sumseries(int n,int current_term)
{
if(n == 1)
return current_term;
next_term = 10 * current_term + 1
return current_term + sumseries(n-1,next_term);
}
main
> sumseries(n,1)
而不只是 sumseries(n)
#include <stdio.h>
int sumseries(int);
int main()
{
int n;
printf("Enter the number: ");
scanf("%d",&n);
printf("The sum of the series is %d",sumseries(n));
}
int sumseries(int n)
{
int i,sum=0;
if(n==1)
return 1;
for(i=0;i<n;i++)
{
sum=sum*10+1;
}
return (sum+sumseries(n-1));
}
1 + 11 + 111 + 1111 + 11111 + 111111
1 + 11 = 12
, 12 + 111 = 123
, 123 + 1111 = 1234
, etc.111111 + 11111 = 122222
, 122222 + 1111 = 123333
, etc.sumseries
to be returning the sums of the series.n
that says how many terms to write, and a current_term
variable that gives the current term?next_term = 10 * current_term + 1
return current_term + sumseries(n-1, next_term);
int sumseries(int n, int current_term)
{
if(n==1)
return current_term;
next_term = 10 * current_term + 1
return current_term + sumseries(n-1, next_term);
}
main
to be sumseries(n, 1)
instead of just sumseries(n)