cor*_*tex 5 c++ for-loop nested
我试图找出如何使用递归来执行n级嵌套for循环.例如,如果n = 3,则会有3个"级别"
for(z=0;z<6;z++){
for(y=0;y<6;y++){
for(x=0;x<6;x++){
if (z+y+x==f){
//do something
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
等等.
我似乎无法弄清楚如何将if循环放在最后一个for循环中以及如何从if语句访问先前for循环的变量.我知道变量嵌套循环的问题已经被问了很多次,我已经查看了所有这些.但似乎没有人帮助我.
有人能提出一种简单的方法来使用递归来实现这一点,记住我仍然是c ++的初学者,指出我正确的方向吗?
用例如下:
编写一个程序来输入骰子数m.程序将输出可能情况的总数,每个可能的n的可能情况的数量和具有最高概率的n.注意:只读入一个输入m.n由程序计算
例如,如果用户输入m = 2,则应输出程序
可能的病例总数为36.
可能性为
2 1
3 2
4 3
.
.
.
12 1
小智 10
我今天早些时候遇到过这个,并认为我可能会分享我最终提出的解决方案.我不确定回复旧职位的政策是什么.我只是因为我今天早上遇到了这个问题,这种事情对我有用.
为了提高效率,我避免了递归.此外,它不使用任何特定的c ++东西 - 它也可以在C上正常工作.
我们正在尝试创建N个嵌套的"for"循环.而不是使用
for(int i = 0; i<max; i++)
for (int j = 0; j<max; j++)
...
Run Code Online (Sandbox Code Playgroud)
我将用数组替换i,j,...:i [0],i [1],...,i [n-1].
这是我的解决方案:
const int n = /*Insert N here: how many loops do you need?*/;
int i[n+1]; // if "n" is not known before hand, then this array will need to be created dynamically.
//Note: there is an extra element at the end of the array, in order to keep track of whether to exit the array.
for (int a=0; a<n+1; a++) {
i[a]=0;
}
int MAX = 79; //That's just an example, if all of the loops are identical: e.g. "for(int i=0; i<79; i++)". If the value of MAX changes for each loop, then make MAX an array instead: (new) int MAX [n]; MAX[0]=10; MAX[1]=20;...;MAX[n-1]=whatever.
int p = 0; //Used to increment all of the indicies correctly, at the end of each loop.
while (i[n]==0) {//Remember, you're only using indicies i[0], ..., i[n-1]. The (n+1)th index, i[n], is just to check whether to the nested loop stuff has finished.
//DO STUFF HERE. Pretend you're inside your nested for loops. The more usual i,j,k,... have been replaced here with i[0], i[1], ..., i[n-1].
//Now, after you've done your stuff, we need to increment all of the indicies correctly.
i[0]++;
// p = 0;//Commented out, because it's replaced by a more efficient alternative below.
while(i[p]==MAX) {//(or "MAX[p]" if each "for" loop is different. Note that from an English point of view, this is more like "if(i[p]==MAX". (Initially i[0]) If this is true, then i[p] is reset to 0, and i[p+1] is incremented.
i[p]=0;
i[++p]++; //increase p by 1, and increase the next (p+1)th index
if(i[p]!=MAX)
p=0;//Alternatively, "p=0" can be inserted above (currently commented-out). This one's more efficient though, since it only resets p when it actually needs to be reset!
}
}
Run Code Online (Sandbox Code Playgroud)
那就是全部.希望这些评论能够清楚地说明它的目的是什么.我认为它应该非常高效 - 几乎和真正的嵌套for循环一样多.大部分开销在开始时是一次性的,所以这应该比使用递归函数等更有效(如果我在这一点上错了,请纠正我).
希望有一天对某人有用.
和平与爱.
具有多个循环的递归算法的基本结构如下:
void recursiveLoops(vector<int>& indexes, const vector<int>& endPerIndex, int currentIndex) {
if (currentIndex == indexes.size()) {
// This is where the real logic goes.
// indexes[i] contain the value of the i-th index.
} else {
for (indexes[pos] = 0 ; indexes[pos] != endPerIndex[pos] ; indexes[pos]++) {
// Recurse for the next level
recursiveLoops(indexes, endPerIndex, pos+1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
recursiveLoops从顶层调用的设置需要两个向量 - 一个用于索引,一个用于每个级别的迭代次数.下面的示例设置了三个嵌套循环,每个级别迭代5次,6次和9次:
vector<int> indexes(3, 0);
vector<int> endPerIndex;
endPerIndex.push_back(5);
endPerIndex.push_back(6);
endPerIndex.push_back(9);
recursiveLoops(indexes, endPerIndex, 0);
Run Code Online (Sandbox Code Playgroud)
这是一个普通的旧 C++ 示例。首先,我为每个维度创建一个范围向量,称为maxes。如果所有索引的总和为 2,那么我打印做了一些事情。在示例中,我将 z 从 0 循环到 1,将 y 从 0 循环到 2,将 x 从 0 循环到 3
你肯定可以让这更整洁。
开始:
#include <iostream>
#include <vector>
using namespace std;
int f(){
return 2 ;
}
void inner(int depth,vector<int> & numbers,vector<int> & maxes){
if (depth>0){
for(int i=0;i<maxes[depth-1];i++){
numbers[depth-1]=i;
inner(depth-1, numbers,maxes) ;
}
}else{
// calculate sum of x,y,z:
cout << "values are ";
for(int i=0;i<numbers.size();i++){
cout <<numbers[i]<<" ";
}
int thesum(0);
for(int i=0;i<numbers.size();i++){
thesum+=numbers[i];
}
if (thesum==f()){
cout << "did something! ";
}
cout<<endl;
}
}
void donest(){
vector<int> numbers;
numbers.resize(3);
vector<int> maxes;
maxes.push_back(4);
maxes.push_back(3);
maxes.push_back(2);
inner(numbers.size(),numbers,maxes);
}
int main(){
donest();
}
Run Code Online (Sandbox Code Playgroud)
结果:
values are 0 0 0
values are 1 0 0
values are 2 0 0 did something!
values are 3 0 0
values are 0 1 0
values are 1 1 0 did something!
values are 2 1 0
values are 3 1 0
values are 0 2 0 did something!
values are 1 2 0
values are 2 2 0
values are 3 2 0
values are 0 0 1
values are 1 0 1 did something!
values are 2 0 1
values are 3 0 1
values are 0 1 1 did something!
values are 1 1 1
values are 2 1 1
values are 3 1 1
values are 0 2 1
values are 1 2 1
values are 2 2 1
values are 3 2 1
Run Code Online (Sandbox Code Playgroud)