Return to Snippet

Revision: 43640
at March 28, 2011 18:34 by dirkchang


Updated Code
/*
 * combination.cpp
 *
 *  Created on: 2011/3/25
 *      Author: dirk
 */
// for convenience, indexing in this alg. starts from 1

#include <iostream>
using namespace std;

#define MAX 512

inline int advance_and_relax(int digits[], int m, int n) {
    if(n == 1) return 0; // no way to advance
    for(int i = n - 1; i >= 1; --i) {
        if(++digits[i] <= (m - n + i)) { // digits[i] is able to advance
            for(int cur_index = i+1, cur_value = digits[i]+1; cur_index <= n; ++cur_index) { // we relax all digits behind digits[i]
                digits[cur_index] = cur_value++;
            }   
            return 1;
        }   
    }   
    return 0;
}

inline void C(int x[], int m, int n) {
    int digits[MAX+1]; // our indexing starts from 1
    for(int i = 0; i < n + 1; ++i) digits[i] = i; // initialization
    int finish = 0;
    while(finish == 0) {
        for(int i = 1; i <= n; ++i) cout << x[digits[i]] << ' ';
        cout << '\n';
        if(++digits[n] > m) {
            if(advance_and_relax(digits, m, n) == 1) finish = 0;
            else finish = 1;
        }   
    }   
}

int main() {
    int m, n;
    int x[MAX+1];
    cout << "M = ? ";
    cin >> m;
    cout << "N = ? ";
    cin >> n;
    cout << "輸入 M 個數字: ";
    for(int i = 1; i <= m; ++i) cin >> x[i];
    if(m < n || m <=0 || n <= 0 || m > 512) {
        cout << "輸入格式不正確, 界線條件是 M > N 且 M, N > 0\n"
             << "記憶體限制是 M 必須小於等於 512\n"
             << "真的 M 太大也會算很久怕你沒耐心XD\n";
        return -1;
    }
    C(x, m, n);
}

Revision: 43639
at March 28, 2011 18:30 by dirkchang


Updated Code
/*
 * combination.cpp
 *
 *  Created on: 2011/3/25
 *      Author: dirk
 */
// for convenience, indexing in this alg. starts from 1

#include <iostream>
using namespace std;

#define MAX 512

inline int advance_and_relax(int digits[], int m, int n) {
    if(n == 1) return 0; // no way to advance
    for(int i = n - 1; i >= 1; --i) {
        if(++digits[i] <= (m - n + i)) { // digits[i] is able to advance
            for(int cur_index = i+1, cur_value = digits[i]+1; cur_index <= n; ++cur_index) { // we relax all digits behind digits[i]
                digits[cur_index] = cur_value++;
            }   
            return 1;
        }   
    }   
    return 0;
}

inline void C(int x[], int m, int n) {
    int digits[MAX+1]; // our indexing starts from 1
    for(int i = 0; i < n + 1; ++i) digits[i] = i; // initialization
    int finish = 0;
    while(finish == 0) {
        for(int i = 1; i <= n; ++i) cout << x[digits[i]] << ' ';
        cout << '\n';
        if(++digits[n] > m) {
            if(advance_and_relax(digits, m, n) == 1) finish = 0;
            else finish = 1;
        }   
    }   
}

int main(int argc, char **argv) {
    int m, n;
    int x[MAX+1];
    cout << "M = ? ";
    cin >> m;
    cout << "N = ? ";
    cin >> n;
    cout << "輸入 M 個數字: ";
    for(int i = 1; i <= m; ++i) cin >> x[i];
    if(m < n || m <=0 || n <= 0 || m > 512) {
        cout << "輸入格式不正確, 界線條件是 M > N 且 M, N > 0\n"
             << "記憶體限制是 M 必須小於等於 512\n"
             << "真的 M 太大也會算很久怕你沒耐心XD\n";
        return -1;
    }
    C(x, m, n);
}

Revision: 43638
at March 28, 2011 18:28 by dirkchang


Updated Code
/*
 * combination.cpp
 *
 *  Created on: 2011/3/25
 *      Author: dirk
 */
// for convenience, indexing in this alg. starts from 1

#include <iostream>
using namespace std;

#define MAX 512

inline int advance_and_relax(int digits[], int m, int n) {
    if(n == 1) return 0; // no way to advance
    for(int i = n - 1; i >= 1; --i) {
        if(++digits[i] <= (m - n + i)) { // digits[i] is able to advance
            for(int cur_index = i+1, cur_value = digits[i]+1; cur_index <= n; ++cur_index) { // we relax all digits behind digits[i]
                digits[cur_index] = cur_value++;
            }   
            return 1;
        }   
    }   
    return 0;
}

inline void C(int x[], int m, int n) {
    int digits[MAX+1]; // our indexing starts from 1
    for(int i = 0; i < n + 1; ++i) digits[i] = i; // initialization
    int finish = 0;
    while(finish == 0) {
        for(int i = 1; i <= n; ++i) cout << x[digits[i]] << ' ';
        cout << '\n';
        if(++digits[n] > m) {
            if(advance_and_relax(digits, m, n) == 1) finish = 0;
            else finish = 1;
        }   
    }   
}

int main() {
    int m, n;
    int x[MAX+1];
    cout << "M = ? ";
    cin >> m;
    cout << "N = ? ";
    cin >> n;
    cout << "輸� M ���: ";
    for(int i = 1; i <= m; ++i) cin >> x[i];
    if(m < n || m <=0 || n <= 0 || m > 512) {
        cout << "輸�格��正確, ���件� M > N � M, N > 0\n"
             << "������ M ������ 512\n"
             << "�� M 太大������你���XD\n";
        return -1; 
    }   
    C(x, m, n); 
}

Revision: 43637
at March 28, 2011 18:22 by dirkchang


Updated Code
/*
 * combination.cpp
 *
 *  Created on: 2011/3/25
 *      Author: dirk
 */
// for convenience, indexing in this alg. starts from 1

#include <iostream>
using namespace std;

#define MAX 512

inline bool advance_and_relax(int digits[], int m, int n) {
	if(n == 1) return false; // no way to advance
	for(int i = n - 1; i >= 1; --i) {
		if(++digits[i] <= (m - n + i)) { // digits[i] is able to advance
			for(int cur_index = i+1, cur_value = digits[i]+1; cur_index <= n; ++cur_index) { // we relax all digits behind digits[i]
				digits[cur_index] = cur_value++;
			}
			return true;
		}
	}
	return false;
}

inline void C(int x[], int m, int n) {
	int digits[MAX+1]; // our indexing starts from 1
	for(int i = 0; i < n + 1; ++i) digits[i] = i; // initialization
	bool finish = false;
	while(!finish) {
		for(int i = 1; i <= n; ++i) cout << x[digits[i]] << ' ';
		cout << '\n';
		if(++digits[n] > m) finish = !advance_and_relax(digits, m, n);
	}
}

int main() {
    int m, n;
    int x[MAX+1];
    cout << "M = ? ";
    cin >> m;
    cout << "N = ? ";
    cin >> n;
    cout << "輸入 M 個數字: ";
    for(int i = 1; i <= m; ++i) cin >> x[i];
    if(m < n || m <=0 || n <= 0 || m > 512) {
        cout << "輸入格式不正確, 界線條件是 M > N 且 M, N > 0\n"
             << "記憶體限制是 M 必須小於等於 512\n"
             << "真的 M 太大也會算很久怕你沒耐心XD\n";
        return -1; 
    }   
    C(x, m, n); 
}

Revision: 43636
at March 28, 2011 18:21 by dirkchang


Initial Code
/*
 * combination.cpp
 *
 *  Created on: 2011/3/25
 *      Author: dirk
 */
// for convenience, indexing in this alg. starts from 1

#include <iostream>
using namespace std;

#define MAX 512

inline bool advance_and_relax(int digits[], int m, int n) {
	if(n == 1) return false; // no way to advance
	for(int i = n - 1; i >= 1; --i) {
		if(++digits[i] <= (m - n + i)) { // digits[i] is able to advance
			for(int cur_index = i+1, cur_value = digits[i]+1; cur_index <= n; ++cur_index) { // we relax all digits behind digits[i]
				digits[cur_index] = cur_value++;
			}
			return true;
		}
	}
	return false;
}

inline void C(int x[], int m, int n) {
	int digits[MAX+1]; // our indexing starts from 1
	for(int i = 0; i < n + 1; ++i) digits[i] = i; // initialization
	bool finish = false;
	while(!finish) {
		for(int i = 1; i <= n; ++i) cout << x[digits[i]] << ' ';
		cout << '\n';
		if(++digits[n] > m) finish = !advance_and_relax(digits, m, n);
	}
}

int main(int argc, char **argv) {
	int m, n;
	int x[MAX+1];
	cout << "M = ? ";
	cin >> m;
	cout << "N = ? ";
	cin >> n;
	cout << "輸� M ���: ";
	for(int i = 1; i <= m; ++i) cin >> x[i];
	if(m < n || m <=0 || n <= 0 || m > 512) {
		cout << "輸�格��正確, ���件� M > N � M, N > 0\n"
		     << "������ M ������ 512\n"
			 << "�� M 太大������你���XD\n";
		return -1;
	}
	C(x, m, n);
}

Initial URL


Initial Description


Initial Title
Combination (no pointer, only array)

Initial Tags


Initial Language
C++