本文介紹了創建 N 個嵌套的 for 循環的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
有沒有辦法創建表單的for循環
Is there a way to create for-loops of a form
for(int i = 0; i < 9; ++i) {
for(int j = 0; j < 9; ++i) {
//...
for(int k = 0; k < 9; ++k) { //N-th loop
在編譯時不知道 N.理想情況下,如果一定數量的數字被不同的數字替換,我試圖找出一種方法來循環遍歷數字向量的單獨元素以創建每個可能的數字.
without knowing N at the compile time. Ideally I'm trying to figure out a way to loop through separate elements of a vector of digits to create each possible number if a certain amount of digits is replaced with different digits.
推薦答案
您可以使用遞歸代替基本條件 -
You may use recursion instead with a base condition -
void doRecursion(int baseCondition){
if(baseCondition==0) return;
//place your code here
doRecursion(baseCondition-1);
}
現在您不需要在編譯時提供 baseCondition
值.您可以在調用 doRecursion()
方法時提供它.
Now you don't need to provide the baseCondition
value at compile time. You can provide it while calling the doRecursion()
method.
這篇關于創建 N 個嵌套的 for 循環的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!