c++ - Editreturn 我不太明白这里问的递归函数的概念 (c++)

标签 c++ algorithm function recursion

我目前正在用 C++ 做一些练习,但不知何故我无法解决这个问题:

Create a function named bin that, that takes two integer arguments: n and k, and returns an integer. If k is equal to 0 or n is smaller than k, this function has to return 1. In any other case, this function should return the sum of bin(n-1, k-1) and bin(n-1, k).

形式为 int bin (int x, int y) 的函数, 与 if (n == 0 || n"<"k) { return 1;}else { bin (n-1, k-1); bin (n-1, k),else bin之前被创建为 bin ( double x, double y) { return x+y; .

或者类似的东西。我不能完全确定任务的目标。

`    int bin(int n, int k)
{
if (k == 0 || n<k)
{
    return 1;
}
else
{
    cout<< bin (n-1, k-1) + bin (n-1, k);
}
}

int main ()
{
int n;
int k;
cout <<" Please enter a value for n\n";
cin >> n;
cout << "Please enter a value for k\n";
cin >> k;
int result = bin (n, k);
cout << result;

} `

编辑:看来我从一开始就掌握了大部分内容,而且真的和我想的一样简单,也许是心理障碍。

但是谢谢大家。我认为我从未体验过如此快速高效的社区,也许有一天我会在其他问题上派上用场。

最佳答案

我认为这只是将英语翻译成 C++ 的问题,不是吗?

// Create a function named bin that, that takes two integer arguments: n and k, 
//and returns an integer. 
int bin(int n, int k)
{
  // If k is equal to 0 or n is smaller than k, this function has to return 1. 
  if ((k == 0) || n < k)
    return 1;

  //In any other case, this function should return the sum of 
  // bin(n-1, k-1) and bin(n-1, k).
  return bin(n-1, k-1) + bin(n-1, k);
}

关于c++ - Editreturn 我不太明白这里问的递归函数的概念 (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41635792/

相关文章:

algorithm - 递归查询邻接表以在 SQL 中预先排序树遍历?

algorithm - Google Shopper 中的图像识别是如何工作的?

R函数删除列中的前4个字符?

c - 为什么嵌套函数可以在turboc++中工作,但不能在gnug++中工作?

C++:生成高斯分布

java - 返回区域的查找算法?

c++ - 有很多读者时使用 pthread_rwlock 的效率

php - 如何在函数选择中使用IF和ELSE?

c++ - 如何在函数内部创建一个数组,然后使用该数组创建另一个数组?

c++ - 从 'const char**' 到 'char* const*' 的无效转换