5

# 問題描述

``````#include <iostream>
using namespace std;

//函數功能: 單純把陣列的前n個數印出來做測試
void printArray(int* array, int n)
{
for (int i = 0; i < n; i++) {
cout << array[i] << " ";
}
cout << endl;
}

int main(void)
{
int array[4];
int A, B, C, D;
for(A=2; A<=5; A++)
for(B=2; B<=5; B++)
for(C=2; C<=5; C++)
for(D=2; D<=5; D++)
{
array[0] = A;
array[1] = B;
array[2] = C;
array[3] = D;
printArray(array, 4);
}
return 0;
}
``````

### 7 個回答

9

iT邦大師 1 級 ‧ 2020-05-14 23:40:36

https://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists

1

``````X = callfunc(...);

function callfunc(...){
//判斷再call
X = callback(...);
return X;
}

``````

1
japhenchen
iT邦大師 1 級 ‧ 2020-05-15 08:50:23

``````    foreach( var item in array )
{
item = 1
Console.WriteLine(item);
}
``````

``````    for item in array:
item = 1
print(item)
``````

``````   ccc = 1
for item in array:
item = ccc
print(item)
ccc += 1

``````
0
dscwferp
iT邦高手 1 級 ‧ 2020-05-15 09:08:14

2

iT邦大神 1 級 ‧ 2020-05-15 09:10:27

python 的 itertools

(避免若陣列中有8個元素，就真的要寫8個for迴圈)

2

iT邦研究生 2 級 ‧ 2020-05-15 10:32:14

``````#include<vector>

class StatusManger
{
private:
std::vector<int> status;
std::vector<int> limits;
public:
void push(int n);
void init();
bool next();
int get(int x);
int size();
};

void StatusManger::push(int n)
{
limits.push_back(n);
}

void StatusManger::init()
{
status.clear();
for(int i=limits.size();i>0;--i) {
status.push_back(0);
}
}

bool StatusManger::next()
{
int carry=1,
current=0,
n=limits.size();
while(carry && current<n) {
if(status[current]+1<limits[current]) {
++status[current];
carry=0;
} else {
status[current]=0;
}
++current;
}
return carry==0?true:false;
}

int StatusManger::get(int x)
{
if(x<0 || x>=status.size()) {
return -1;
}
return status[x];
}

int StatusManger::size()
{
return limits.size();
}
``````

``````int main()
{
StatusManger sm;
sm.push(4); //第一層，會產生 0~3
sm.push(4); //第二層，會產生 0~3
sm.push(4); //第三層，會產生 0~3
sm.push(4); //第四層，會產生 0~3
int n=sm.size(); //取得共幾層
sm.init(); //初始化狀態
do{
for(int i=0;i<n;++i) {
//輸出時加上2就可以了
std::cout<<sm.get(i)+2;
}
std::cout<<std::endl;
}while(sm.next()); //如果還有下一個 next 會回傳 true 並更新狀態
return 0;
}
``````

``````int main()
{
const char* strArr[3]={"xyz", "1234", "AB"};
StatusManger sm;
sm.push(strlen(strArr[0]));
sm.push(strlen(strArr[1]));
sm.push(strlen(strArr[2]));
int n=sm.size();
sm.init();
do{
for(int i=0;i<n;++i) {
std::cout<<strArr[i][sm.get(i)];
}
std::cout<<std::endl;
}while(sm.next());
return 0;
}
``````

1
CP-Y
iT邦新手 5 級 ‧ 2020-05-15 11:21:59

``````#include <iostream>

using namespace std;

// 輸出
void printArray(int* array, int n)
{
for (int i = 0; i < n; i++) {
cout << array[i] << " ";
}
cout << endl;
}

// 遞迴式
void Trace(int min, int max, int length, int index, int* arr)
{
// 根據長度初始化
if(arr == NULL)
{
int array[length];
for (int i = 0; i < length; i++)
array[i] = min;

arr = array;
}

// 遞迴結束點
if(index >= length)
{
printArray(arr, length);
return;
}

for (int j = min; j <= max; j++)
{
arr[index] = j;
Trace(min, max, length, index + 1 ,arr);
}
}

int main()
{
int length = 4;
int min = 2;
int max = 5;
printf("min = %d, max = %d, length = %d\n", min, max, length);
Trace(min, max, length, 0, NULL);
return 0;
}
``````