I need to generate all 10-digit telephone numbers which start with 0721 and have 3 distinct even digits (not counting the first 4). I know I should be using backtracking and I think I got the math behind this: out of the 6 remaining digits, we should have combinations of 5 taken as 3 (0, 2, 4, 6, 8) * combinations with repetitions of 5 taken as 3 (1, 3, 5, 7, 9). I'm just not sure how to write this in code as my valid function.
Code:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define N 6 // because we already know the first 4 digits
int v[N], even[5] = {0, 2, 4, 6, 8}, odd[5] = {1, 3, 5, 7, 9};
void display()
{
FILE *fout = fopen("telephone_numbers.txt", "w");
if(!fout)
{
fprintf(stderr, "Error opening the file.\n");
exit(EXIT_FAILURE);
}
fprintf(fout, "0721 ");
for(int i = 0; i < N; i++)
{
fprintf(fout, "%d", v[i]);
}
fputc(';', fout);
}
bool valid(int step, int v[])
{
int i;
for(i = 1; i < step; i++)
{
if(
}
}
bool solution(step)
{
if(step == N)
return true;
return false;
}
void back(int step)
{
for(int i = 0; i < N; i++)
{
if(valid(step))
{
if(solution(step))
{
display();
}
else back(step + 1);
}
}
}
int main(int argc, char **argv)
{
back(1);
return 0;
}