USACO OPEN07 Problem 'count' Analysis

by Rob Kolstad

This task asks for a program that will sequentially count through integers whose representation does not contain a certain digit. A 'seendigit' function makes the mainline easy; it returns the boolean value that answer the question, "Does this integer contain the digit l?"

The seendigit function peels digits off the right side of the number, testing and then discarding them. When the number finally reaches 0, all the digits are peeled and a proper return value can be sent back.

#include <stdio.h>
 
int l;
 
int seendigit (n) {
    for ( ; n; n /= 10)
        if (n % 10 == l) return 1;
    return 0;
}

main () {
    FILE *fin = fopen ("count.in", "r");
    FILE *fout = fopen ("count.out", "w");
    int i, n, counter = 1;

    fscanf (fin, "%d %d", &n, &l);
    for (i = 0; i < n; i++, counter++)
        while (seendigit(counter))
            counter++;
    fprintf (fout, "%d\n", counter-1);
    exit (0);
}