[C/C++ 演算法]- 產生可能的清單
剛才找資料時發現一個C/C++的教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。
拷貝來源:
http://openhome.cc/Gossip/AlgorithmGossip/
http://openhome.cc/Gossip/AlgorithmGossip/PossibleSet.htm
#include <stdio.h> #include <stdlib.h>
#define MAXSIZE 20
int indexOf(int, int*, int); void cleanTo(int, int*); int hasNext(int*, int); void next(int*, int); void printList(int*, int);
int main(void) { int digits[MAXSIZE] = {0}; int length; printf("輸入清單個數:"); scanf("%d", &length);
printList(digits, length); while(hasNext(digits, length)) { next(digits, length); printList(digits, length); }
return 0; }
int indexOf(int n, int* digits, int length) { int i; for(i = 0; i < length && digits[i] != n; i++); return i == length ? -1 : i; }
void cleanTo(int i, int* digits) { int j; for(j = 0; j < i; digits[j] = 0, j++); }
int hasNext(int* digits, int length) { return indexOf(0, digits, length) != -1; }
void next(int* digits, int length) { int i = indexOf(0, digits, length); cleanTo(i, digits); digits[i] = 1; }
void printList(int* digits, int length) { int i = indexOf(1, digits, length); printf(i == -1 ? "[" : "[%d", i + 1); int j; for(j = i + 1; j < length; j++) if(digits[j] == 1) { printf(", %d", j + 1); } printf("]\n"); }
|
沒有留言:
張貼留言