久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

變量嵌套 for 循環(huán)

variable nested for loops(變量嵌套 for 循環(huán))
本文介紹了變量嵌套 for 循環(huán)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我想弄清楚如何使用遞歸來(lái)執(zhí)行 n 級(jí)嵌套 for 循環(huán).例如,如果 n=3,就會(huì)有 3 個(gè)級(jí)別"

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
         } 
      }
   }
}

等等.

我似乎無(wú)法弄清楚如何將 if 循環(huán)放置在最后一個(gè) for 循環(huán)中,以及如何從 if 語(yǔ)句訪問(wèn)前一個(gè) for 循環(huán)的變量.我知道變量嵌套循環(huán)的問(wèn)題已經(jīng)被問(wèn)過(guò)很多次了,我已經(jīng)瀏覽了所有這些問(wèn)題.但似乎沒(méi)有人幫助我.

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.

有人可以提出一種使用遞歸來(lái)實(shí)現(xiàn)這一目標(biāo)的簡(jiǎn)單方法,請(qǐng)記住我仍然是 C++ 的初學(xué)者,為我指明正確的方向嗎?

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?

用例如下:

編寫程序輸入骰子的個(gè)數(shù)m.程序?qū)⑤敵隹赡馨咐目倲?shù)、每個(gè)可能 n 的可能案例數(shù)量以及具有最高概率的 n.注意:只有一個(gè)輸入 m 被讀入. n 由程序計(jì)算

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

示例如果用戶輸入 m=2 則程序應(yīng)輸出

Example if user enters m=2 then program should output

可能的病例總數(shù)為 36.
可能性是
2 1
3 2
4 3
.
.
.
12 1

The total number of possible cases is 36.
The possibilities are
2 1
3 2
4 3
.
.
.
12 1

推薦答案

這是一個(gè)普通的舊 C++ 示例.首先,我為每個(gè)維度創(chuàng)建一個(gè)范圍向量,稱為 maxes.如果所有索引的總和為 2,那么我打印做了一些事情.在示例中,我將 z 從 0 循環(huán)到 1,將 y 從 0 循環(huán)到 2,將 x 從 0 循環(huán)到 3

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.

這里是:

#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();
}

結(jié)果:

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 

這篇關(guān)于變量嵌套 for 循環(huán)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時(shí)分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運(yùn)算符的數(shù)學(xué)式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區(qū)別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達(dá)式中的變量聲明)
主站蜘蛛池模板: 国产成人麻豆免费观看 | 久久四虎| 亚洲欧美日本在线 | 怡红院免费的全部视频 | 欧美11一13sex性hd | 99精品免费久久久久久久久日本 | 精品一区二区三区在线视频 | 亚洲永久 | 天天操天天摸天天爽 | 国产一区精品 | 日本亚洲一区 | 9999在线视频 | 欧美成人激情 | 99re在线视频免费观看 | 精品久久精品 | 成人水多啪啪片 | 日韩在线不卡 | 夜夜骑天天干 | 日韩av福利在线观看 | 久操国产 | 福利视频网站 | 精品一区二区视频 | 久久蜜桃av一区二区天堂 | 国产综合精品一区二区三区 | 日韩中文一区 | 久久久亚洲综合 | 免费av播放 | 欧美日韩一二三区 | 国产精品亚洲一区 | 一级毛片视频在线 | 天天天插 | av免费网站在线观看 | 特级特黄特色的免费大片 | 亚洲高清av | 欧美一区二区免费视频 | 91精品国产91 | 亚洲福利在线观看 | 五月婷婷视频 | 日韩av电影院 | 国产九九九九 | 国产亚洲成av人片在线观看桃 |