gpt4 book ai didi

java - 具有头部和尾部的可变数量的嵌套循环

转载 作者:行者123 更新时间:2023-11-30 02:41:56 24 4
gpt4 key购买 nike

我有这些嵌套的 for 循环:

void fun() {

int n = 5;
int c = 0;

for (int i = 0; i < n; i++) {
head_function(i, c);
for (int j = 0; j < n; j++) {
head_function(j, c);
for (int w = 0; w < n; w++) {
head_function(w, c);
for (int x = 0; x < n; x++) {
head_function(x, c);

c++;
body(c);

tail_function(x, c);
}
tail_function(w, c);
}
tail_function(j, c);
}
tail_function(i, c);
}
}

head 和 tail 函数做什么并不重要,只要它们能够跟踪它们的索引 i、j、w、x 即可。

我想要的是任意数量的嵌套 for 循环,而不是只有四个。

我在这里找到的其他解决方案并不真正适合我,因为我猜它们不包括 head 和 tail 函数。

最佳答案

这是一个可以帮助您入门的框架。您可以根据需要随意添加参数并更改签名。

public void doStuffRecursively(int nTimes){
doIt(nTimes);
}

void doIt(int depth){
if(depth==0)
body();
else{
head(depth);
doIt(depth-1);
tail(depth);
}
}

void body(){}

void head(int depth){}
void tail(int depth){}

关于java - 具有头部和尾部的可变数量的嵌套循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41430083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com