C void函数中的递归过程
我刚刚在stackoverflow中找到了一个代码片段(python)。由于着迷,我将代码片段翻译成C语言(当然是个错误) 我的代码片段C void函数中的递归过程,c,algorithm,recursion,void,backtracking,C,Algorithm,Recursion,Void,Backtracking,我刚刚在stackoverflow中找到了一个代码片段(python)。由于着迷,我将代码片段翻译成C语言(当然是个错误) 我的代码片段 #include <stdio.h> #include <stdlib.h> int top = 0; void print(int *partial) { int j =0; while(partial[j] != 0) { printf("%d ",
#include <stdio.h>
#include <stdlib.h>
int top = 0;
void print(int *partial)
{
int j =0;
while(partial[j] != 0)
{
printf("%d ",partial[j]);
j++;
}
}
void subset(int *nos,int *partial,int reqvalue,int maxvalue) {
int par_sum = 0,i = 0,count=0;
while(partial[i] != 0)
{
par_sum += partial[i];
i++;
}
if(par_sum == reqvalue)
{
print(partial);
}
if(par_sum > reqvalue)
{
return ;
}
while(nos[count]!= 0)
{
count++;
}
for(int j =0; j < count; j++)
{
int n;
int remaining[count+1];
n = nos[j];
for(int k = j+1; k < count+1; k++) {
remaining[k-(j+1)] = nos[k];
}
partial[top] = n;
top++;
subset(remaining,partial,reqvalue,maxvalue);
top--;
}
}
int main()
{
int reqvalue,no_count,sum = 0;
int *nos;
int *partial;
scanf("%d %d",&reqvalue,&no_count);
nos = calloc(( no_count+1),sizeof(int));
partial = calloc(no_count, sizeof(int));
for(int i =no_count-1; i>=0; i--)
{
scanf("%d",&nos[i]);
sum += nos[i];
}
subset(nos,partial,reqvalue,sum);
return 0;
}
#Input
#100 10
#4 14 15 18 29 32 36 82 95 95
#Output
#82 18 82 14 4 82 14 4 36 32 18 14
82 18
def subset_sum(numbers, target, partial=[]):
s = sum(partial)
# check if the partial sum is equals to target
if s == target:
print "sum(%s)=%s" % (partial, target)
if s >= target:
return # if we reach the number why bother to continue
for i in range(len(numbers)):
n = numbers[i]
remaining = numbers[i+1:]
subset_sum(remaining, target, partial + [n])
if __name__ == "__main__":
subset_sum([3,9,8,4,5,7,10],15)
#Outputs:
#sum([3, 8, 4])=15
#sum([3, 5, 7])=15
#sum([8, 7])=15
#sum([5, 10])=15
原始python代码片段
#include <stdio.h>
#include <stdlib.h>
int top = 0;
void print(int *partial)
{
int j =0;
while(partial[j] != 0)
{
printf("%d ",partial[j]);
j++;
}
}
void subset(int *nos,int *partial,int reqvalue,int maxvalue) {
int par_sum = 0,i = 0,count=0;
while(partial[i] != 0)
{
par_sum += partial[i];
i++;
}
if(par_sum == reqvalue)
{
print(partial);
}
if(par_sum > reqvalue)
{
return ;
}
while(nos[count]!= 0)
{
count++;
}
for(int j =0; j < count; j++)
{
int n;
int remaining[count+1];
n = nos[j];
for(int k = j+1; k < count+1; k++) {
remaining[k-(j+1)] = nos[k];
}
partial[top] = n;
top++;
subset(remaining,partial,reqvalue,maxvalue);
top--;
}
}
int main()
{
int reqvalue,no_count,sum = 0;
int *nos;
int *partial;
scanf("%d %d",&reqvalue,&no_count);
nos = calloc(( no_count+1),sizeof(int));
partial = calloc(no_count, sizeof(int));
for(int i =no_count-1; i>=0; i--)
{
scanf("%d",&nos[i]);
sum += nos[i];
}
subset(nos,partial,reqvalue,sum);
return 0;
}
#Input
#100 10
#4 14 15 18 29 32 36 82 95 95
#Output
#82 18 82 14 4 82 14 4 36 32 18 14
82 18
def subset_sum(numbers, target, partial=[]):
s = sum(partial)
# check if the partial sum is equals to target
if s == target:
print "sum(%s)=%s" % (partial, target)
if s >= target:
return # if we reach the number why bother to continue
for i in range(len(numbers)):
n = numbers[i]
remaining = numbers[i+1:]
subset_sum(remaining, target, partial + [n])
if __name__ == "__main__":
subset_sum([3,9,8,4,5,7,10],15)
#Outputs:
#sum([3, 8, 4])=15
#sum([3, 5, 7])=15
#sum([8, 7])=15
#sum([5, 10])=15
提前感谢假设c的翻译是正确的,您只输出子句中的子集
if(par_sum==reqvalue)
打印(部分);
因此,要仅打印第一个,您可以在本条款末尾返回,并确保的循环也会中断,例如,通过使用子集
的返回代码(例如,将其更改为int
),如下所示:
#在子集之前定义中断循环1//
int子集(int*nos、int*partial、int-reqvalue、int-maxvalue)
{
...
修改output子句以返回BREAK\u LOOP
,在另一种情况下返回0:
if(par_sum==reqvalue)
{
打印(部分);
回程中断环;
}
如果(金额>需求值)
返回0;
修改for循环以检查子集
返回值,否则返回0
int subset_ret_val=0;
对于(int j=0;j
如果您可以/不愿意将函数的返回类型更改为int
,则可以等效地传递另一个指针参数。如果您根本无法修改函数签名,您可能仍然可以滥用现有参数进行一些黑客攻击,但您似乎没有为此请求解决方案