如何使用recusion和count排列创建函数 [英] How do I create a function using recusion and counting permutations

查看:100
本文介绍了如何使用recusion和count排列创建函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在上课,我不太明白,我想知道是否有人可以帮我解决这个问题



let c(n )是可以从整数1到n-1中选择的不同组整数的数量,以便每组中的整数加起来为n(例如,n = 4 = [1 + 1 + 1 + 1] = [1 + 1 + 2] = [2 + 2])。在以下变体下写出c(n)的递归定义:

a)您计算排列。例如,1,2,1和1,1,2是两组,每组加起来4

b)你忽略排列



这就是我所拥有但我无法弄清楚如何解决它:



We are going over recursion in my class and I do not quite understand it, I was wondering if someone can help me with this problem

let c(n) be the number of different group integers that can be chosen from the integers 1 through n-1, so that the integers in each group add up to n (for example, n=4=[1+1+1+1]=[1+1+2]=[2+2]). Write a recursive definition for c(n) under the following variations:
a) You count permutations. For example, 1,2,1 and 1,1,2 are two groups that each add up to 4
b)you ignore permutations

This is what I have but I cannot figure out how to fix it:

int recurse_perm(int n)
    {
    	if (n == 1){
    		return 1;
    	}
    	int sum = 1;
        for(int i=n; i>1; i--){
                sum+=1;
                recurse_perm(n+1)+recurse_perm(n);
        }
    	return sum;
    }

推荐答案

为什么你认为这段代码应该有用?

(你写的吧做这个任务,我认为你认为它会起作用。)



目前,忘了代码。

当我是TA在加州理工学院的一个编程课程中,我提出的第一件事是:

如果你不能用英语(或你的母语)写你正在做的事情那么你就不能用代码。

所以要解决这个问题的几个/几个例子(两个必需的版本)。

写下你的假设!

记下你正在采取的每一步。

当你有一个可以使用不同输入的程序时,

然后和只有这样)开始编码。

代码应该反映你所写的程序。

不要担心效率。

你正在寻找正确的行为。

有效地做错误的事情仍然是错误的。

并且可能更难理解为什么这是错误的。
Why do you think this code should work?
(You wrote it to do this assignment, I presume you thought it would work.)

For the moment, forget the code.
When I was TA of a programming class at Caltech, one of the first things I presented was:
If you can't write what you are doing in English (or your native language) then you can't write it in code.
So work through a couple/few of examples of this problem (both of the required versions).
Write down your assumptions!
Write down every step you are taking.
When you have a procedure that will work with different inputs,
then (and only then) start coding.
The code should reflect the procedure you have written.
Do not worry about efficiency yet.
You're looking for correct behavior.
Efficiently doing the wrong thing is still wrong.
And it would probably be harder to understand why it is wrong.


这篇关于如何使用recusion和count排列创建函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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