C笔考题,求高手帮助
C笔试题,求高手帮助
求组合数:求n个数(1,2,...,n)中k个数的组合
如 combination(5, 3)
要求输出:543, 542, 541, 532, 531, 521, 432, 431, 421, 321
------解决方案--------------------
提示:递归
A[n],r[]
初始化A[n]
//用来
bool used[n]
func(n,k,step)
{
if (step == k)
打印
else
依次从A[]中没有用过的数字里面选一个 填到r[step]中
把这个数字标记为用过(使用used[])
func(n,k,step+1)
恢复这个数字把used[]置为0
}
------解决方案--------------------
我以前写过
现在不是在自己的电脑上,过一会贴代码
------解决方案--------------------
------解决方案--------------------
DFS算法
------解决方案--------------------
int check(int cnt)
{
int c = 0;
while(cnt){
cnt &= (cnt-1);
c++;
}
return c;
}
void count()
{
int i;
for(i=7; i<31; i++)
if(check(i) == 3)
output(); *** 根据数写出结果
}
求组合数:求n个数(1,2,...,n)中k个数的组合
如 combination(5, 3)
要求输出:543, 542, 541, 532, 531, 521, 432, 431, 421, 321
------解决方案--------------------
提示:递归
A[n],r[]
初始化A[n]
//用来
bool used[n]
func(n,k,step)
{
if (step == k)
打印
else
依次从A[]中没有用过的数字里面选一个 填到r[step]中
把这个数字标记为用过(使用used[])
func(n,k,step+1)
恢复这个数字把used[]置为0
}
------解决方案--------------------
我以前写过
现在不是在自己的电脑上,过一会贴代码
------解决方案--------------------
------解决方案--------------------
DFS算法
------解决方案--------------------
int check(int cnt)
{
int c = 0;
while(cnt){
cnt &= (cnt-1);
c++;
}
return c;
}
void count()
{
int i;
for(i=7; i<31; i++)
if(check(i) == 3)
output(); *** 根据数写出结果
}