Revision: 43640
Updated Code
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
Updated Code
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
Updated Code
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
Updated Code
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
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
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++