我目前正在用 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/