变量嵌套for循环 [英] variable nested for loops
问题描述
我想弄清楚如何使用递归来做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
}
}
}
}
等等。
我似乎不知道如何放置if循环在最后一个for循环,以及如何从if语句访问前一个for循环的变量。我知道变量嵌套循环的问题已经被问了很多次,我已经查看了所有这些。但没有人似乎帮助我。
有人可以提出一个简单的方法使用递归来实现这一点,记住我还是一个初学者在c + +,指向我的方向正确? / p>
用例如下:
编写程序以输入数字的骰子m。程序将输出可能的情况的总数,每个可能的n的可能情况的数量和具有最高概率的n。注意:只读取一个输入m。n由程序计算
示例如果用户输入m = 2,则程序应输出
可能的案件总数为36.
可能性为:
2 1
3 2
4 3
。
。
。
12 1
下面是一个简单的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 numbers [depth-1] = i;
inner(depth-1,numbers,maxes);
}
} else {
//计算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< 做了什么!
}
cout<< endl;
}
}
void donest(){
vector< int>数字;
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();
}
结果:
值是0 0 0
值是1 0 0
值是2 0 0做了什么!
的值是3 0 0
的值是0 1 0
的值是1 1 0做了什么!
的值是2 1 0
的值是3 1 0
的值是0 2 0做了什么!
的值是1 2 0
的值是2 2 0
的值是3 2 0
的值是0 0 1
的值是1 0 1做了一些事情!
的值是2 0 1
的值是3 0 1
的值是0 1 1做了什么!
的值为1 1 1
的值为2 1 1
的值为3 1 1
的值为0 2 1
的值为1 2 1
的值are 2 2 1
values are 3 2 1
I'm trying to figure out how I can use recursion to do n-level nested for loops. For example, if n=3, there would be 3 'levels'
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
}
}
}
}
and so on.
I can't seem to figure out how I would be able to place the if loop in the last for loop and how I can access the variables of previous for loops from the if statement. I know that the question of variable nested loops has been asked alot of times, and I have looked through all of them. But none seem to help me.
Could someone present an easy way of using recursion to achieve this, keeping in mind that I'm still a beginner in c++, to point me in the right direction?
The use case is as follows:
Write a program to input the number of dice m. The program will output the total number of possible cases, the number of possible cases for each possible n and the n with the highest probability. Note: only one input m is read in. n is computed by the program
Example if user enters m=2 then program should output
The total number of possible cases is 36.
The possibilities are
2 1
3 2
4 3
.
.
.
12 1
Here's an example in plain old C++. First I make a vector of the ranges for each dimension called maxes
. if the sum of all indices are 2 then I print did something.
In the example I loop z from 0 to 1, y from 0 to 2, x from 0 to 3
You can for sure make this more neat.
Here goes:
#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();
}
result:
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
这篇关于变量嵌套for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!