Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define natural unsigned short // csf sa mai pierd timp
- int v[7]; // the stack
- void Init(natural k) { // initialzation function
- v[k] = -1;
- }
- natural Succesor(natural k) {
- if (v[k] < 9) { // will generate the number until 9
- v[k]++;
- return 1;
- }
- return 0;
- }
- natural Valid(natural k) {
- natural i;
- if (v[k] % 2 == 1) return 1; // if is odd is valid
- for (i = 1; i < k; i++) // else we search if is a copy of number in vector
- if (v[i] == v[k])
- return 0; //if we fiind a copy is a invalid combination
- return 1;
- }
- natural valid2() { // we search if are exactly 3 numbers in vector
- natural nr = 0, i;
- for (i = 1; i <= 6; i++) {
- if (v[i] % 2 == 0) nr++;
- }
- if (nr != 3) return 0; // if are not 3 numbers in vector is an invlid case
- return 1;
- }
- natural Solution(natural k) {
- return (k == 6) && valid2(); // if we have 6 elements and 3 of them are disticnt even digits is a valid case
- }
- void Print() {
- natural i;
- printf("0721 "); // fixed digits
- for (i = 1; i <= 6; i++) // digits generated
- printf("%d", v[i]);
- printf(";\n");
- }
- void Back() { // generaly form of bactrack function
- natural k = 1, isS, isV;
- Init(k);
- while (k > 0) {
- isS = 0; isV = 0;
- if (k <= 6)
- do {
- isS = Succesor(k);
- if (isS) isV = Valid(k);
- } while (isS && !isV);
- if (isS)
- if (Solution(k))
- Print();
- else {
- k++;
- Init(k);
- }
- else
- k--;
- }
- }
- int main() {
- Back(); // main call
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement