C++11 置换与组合c++;逻辑错误
这是我的作业,我完成了我的代码,它确实编译了,但它没有给出正确的答案。因此,我知道有一些逻辑错误,但我无法找出是什么 Plz检查并告诉我,计算排列和组合的简单代码C++11 置换与组合c++;逻辑错误,c++11,visual-c++,C++11,Visual C++,这是我的作业,我完成了我的代码,它确实编译了,但它没有给出正确的答案。因此,我知道有一些逻辑错误,但我无法找出是什么 Plz检查并告诉我,计算排列和组合的简单代码 #include <iostream> using namespace std; int factorial(int n) { if (n == 0 or n == 1) { return 1; } else { return n * factor
#include <iostream>
using namespace std;
int factorial(int n)
{
if (n == 0 or n == 1)
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}
int permutation(int a, int b)
{
//factorial(a);
int perm = factorial(a) / factorial(a - b);
return perm;
}
int combination(int a, int b)
{
int permutation(int a, int b);
int factorial(int n);
return permutation(a, b) / factorial(b);;
}
int main()
{
int n;
int r;
cout << "Enter n: " << endl;
cin >> n;
cout << "Enter r: " << endl;
cin >> r;
int factorial(int n);
int permutation(int n, int r);
int combination(int n, int r);
if (n >= r)
{
cout << "Permutuation: " << permutation << endl;
cout << "Combination: " << combination << endl;
}
else
{
cout << "Invalid" << endl;
}
return 0;
}
您的代码中有许多错误。首先,不应在其他函数中重新声明“排列”和“组合”函数 编辑:事实上,这不是一个错误,但在我看来,这是一个非常糟糕的做法。您可能会意外地“隐藏”实际的函数声明,该声明已在任何调用函数之前定义 第二,你的
cout=r){
库特
Enter n:
6
Enter r:
5
Permutuation: 00AC1375
Combination: 00AC133E
#include <iostream>
#include <iso646.h> // Need this in order to use "or" in place of "||"
using namespace std;
int factorial(int n)
{
if (n == 0 or n == 1) {
return 1;
}
else {
return n * factorial(n - 1);
}
}
int permutation(int a, int b)
{
//factorial(a);
int perm = factorial(a) / factorial(a - b);
return perm;
}
int combination(int a, int b)
{
// int permutation(int a, int b); // You don't need to redeclare function inside another one ...
// int factorial(int n);
return permutation(a, b) / factorial(b);;
}
int main()
{
int n, r;
cout << "Enter n: " << endl;
cin >> n;
cout << "Enter r: " << endl;
cin >> r;
// int factorial(int n);
int p = permutation(n, r); // This is how to call your functions ...
int c = combination(n, r); // and assign their returns to values.
if (n >= r) {
cout << "Permutuation: " << p << endl; // Output the values ...
cout << "Combination: " << c << endl; // ...from the functions
}
else {
cout << "Invalid" << endl;
}
return 0;
}