C++ 算法问题 根据输入 计算一个数组中N个数相加的所有组合伪代码int a[20];int number;如果number=n 则算出a中所有元素 n个元素相加 有多少种组合number = 3 算出 a中 3个元素相加 的所有组合
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/31 10:26:40
![C++ 算法问题 根据输入 计算一个数组中N个数相加的所有组合伪代码int a[20];int number;如果number=n 则算出a中所有元素 n个元素相加 有多少种组合number = 3 算出 a中 3个元素相加 的所有组合](/uploads/image/z/1964737-1-7.jpg?t=C%2B%2B+%E7%AE%97%E6%B3%95%E9%97%AE%E9%A2%98+%E6%A0%B9%E6%8D%AE%E8%BE%93%E5%85%A5+%E8%AE%A1%E7%AE%97%E4%B8%80%E4%B8%AA%E6%95%B0%E7%BB%84%E4%B8%ADN%E4%B8%AA%E6%95%B0%E7%9B%B8%E5%8A%A0%E7%9A%84%E6%89%80%E6%9C%89%E7%BB%84%E5%90%88%E4%BC%AA%E4%BB%A3%E7%A0%81int+a%5B20%5D%3Bint+number%3B%E5%A6%82%E6%9E%9Cnumber%3Dn+%E5%88%99%E7%AE%97%E5%87%BAa%E4%B8%AD%E6%89%80%E6%9C%89%E5%85%83%E7%B4%A0+n%E4%B8%AA%E5%85%83%E7%B4%A0%E7%9B%B8%E5%8A%A0+%E6%9C%89%E5%A4%9A%E5%B0%91%E7%A7%8D%E7%BB%84%E5%90%88number+%3D+3+%E7%AE%97%E5%87%BA+a%E4%B8%AD+3%E4%B8%AA%E5%85%83%E7%B4%A0%E7%9B%B8%E5%8A%A0+%E7%9A%84%E6%89%80%E6%9C%89%E7%BB%84%E5%90%88)
C++ 算法问题 根据输入 计算一个数组中N个数相加的所有组合伪代码int a[20];int number;如果number=n 则算出a中所有元素 n个元素相加 有多少种组合number = 3 算出 a中 3个元素相加 的所有组合
C++ 算法问题 根据输入 计算一个数组中N个数相加的所有组合
伪代码
int a[20];
int number;
如果number=n 则算出a中所有元素 n个元素相加 有多少种组合
number = 3 算出 a中 3个元素相加 的所有组合
C++ 算法问题 根据输入 计算一个数组中N个数相加的所有组合伪代码int a[20];int number;如果number=n 则算出a中所有元素 n个元素相加 有多少种组合number = 3 算出 a中 3个元素相加 的所有组合
到底是求组合数目? 还是所有组合?
迭代啊、
int a[20];
int n;
int cnt = 0;
int b[20];
void func( int idx , int depth )
{
if( depth == n ) {
handle(b) // b is a solution
cnt++;
return ;
}
if ( idx < 20 ){
func ( idx+1 , depth ) ; // ignore a[idx]
b[depth] = a[idx];
func ( idx+1 , depth+1 ); // choose a[idx]
}
}