试图写一个递归函数,计数总和到该数字C ++的序列数 [英] trying to write a recursive function that counts the number of sequences that sum up to that number C++

查看:88
本文介绍了试图写一个递归函数,计数总和到该数字C ++的序列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这里是我想要做的。用户输入一个数字。我试图写一个递归函数来计算总和到该数字(用户输入)的序列数。

Okay, so here is what I'm trying to do. The user inputs a number. I'm trying to write a recursive function that counts the number of sequences that sum up to that number (user input).

例如:

那么总和为6的序列数为11(包括6本身)。

Then the number of sequences that sum up to 6 is 11 (including 6 itself).

6
5+1
4+1+1
3+1+1+1
2+1+1+1+1
1+1+1+1+1+1
2+2+1+1
3+2+1
4+2
2+2+2
3+3

我也不想有重复的序列,例如2 + 2 + 1 + 1和1 + 1 + 2 + 2。

I'm also trying not to have sequences that repeat, for example 2+2+1+1 and 1+1+2+2.

没有包含的代码是我无法找出一个递归的方式使这项工作,所以我寻求一些指导。提前感谢!

The reason i have no code included is i cannot figure out a recursive way to make this work so i'm seeking some guidance. Thanks in advance!

添加:

好吧,这就是我的思想过程。
6可以拆分为...

Okay so here is what my thought process is. 6 can be split as...

6
5+1
4+2
3+3

但它还没有结束,如果你取5 + 1并考虑完成+1部分;

but it is still not over,if you take 5+1 and consider the +1 part to be completed; you use the same trick to proceed.

4+1+1
3+2+1

但是他们开始重复.....我不会比我计划中的第二步更进一步。

but then they start to repeat..... and i don't get further than this second step in my plan.

好吧,这样的代码明智这是我已经提出了我自己。寻找修正此问题的建议。

Okay so code wise this is what I've come up with on my own. Looking for suggestions to fix this.

int sum(int number, int min, int counter)
{
    int temp=0, n;
    n=number+temp;
    if (number>=(n/2)& number!=min)
    {
        while (number>=(n/2))
        {
            cout << number << "+"<< temp <<"\n";
            number --;
            temp ++;
            counter ++;
        }
    }
    sum(temp, 1,counter);
    return counter;
}

int main()
{
    int number;

    cout << "Please enter the number: ";
    cin >> number ;
    cout << "\n";

    sum(number, 1, 0);

    return 0;
}



我真的意识到这是各种各样的麻烦。

I do realize this is all sorts of messed up.

推荐答案

提示:尝试找到一个函数,给出总和n 且不大于k 的序列数。

Hint: try to find a function that gives the number of sequences with sum n and terms not larger than k.

编辑:

原谅我,如果这听起来很苛刻,但...更新的代码都错了。很难看到你的意图。我可以猜测,但这是没有意义的。


Forgive me if this sounds harsh, but... your updated code is all wrong. It's hard to see what you intended. I can guess, but that would be pointless.

这里是我想到的:序列应该是非增加的顺序,如2 2 1 1 1 1。那么多少这样的序列加起来6?好吧,找到这样的序列的数目从1开始,然后是从2开始的序列的数目,等等直到6,并将它们相加。多少个序列以2开始,加起来为6? (这是递归的地方。)在每个这样的序列中,第一项是2,其余的加起来4个,没有项超过2 ,因此我们必须找到序列加起来为4,没有大于2的项。所以先写签名,然后是迭代循环,然后递归调用就完成了。

Here's what I had in mind: a sequence should be in nonincreasing order, like "2 2 1 1 1 1". So how many such sequences add up to 6? Well, find the number of such sequences starting with 1, then the number of sequences starting with 2, and so on up to 6, and add them up. And how many sequences start with 2 and add up to six? (This is where the recursion comes in.) In each such sequence, the first term is 2 and the rest add up to 4 with no term exceeding 2, so we must find the number of sequences adding up to 4 with no term greater than 2. So write the signature first, then the iteration loop, then the recursive call and you're done.

编辑:

好,这里的一切,但循环:


All right, here's everything but the loop:

int partition(int n, int max)
{
  if(n==0)
    return(0);
  int ret = 0;
  if(n<=max)
    ret=1;
  for(...)
  {
    ...
  }
  return(ret);
}

您可以填写空格吗?

这篇关于试图写一个递归函数,计数总和到该数字C ++的序列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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