Advertisement
ossifrage

Brute force solution to the self-descriptive number puzzle

Jan 6th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.94 KB | None | 0 0
  1. /*
  2.  * Brute force solution to the self-descriptive number puzzle:
  3.  *
  4.  * I have a ten digit number. The first digit tells me how many zeros are
  5.  * in the number. The second digit tells me how many ones are in the number.
  6.  * The third digit tells me how many twos are in the number, and so on until
  7.  * the tenth digit which tells me how many nines are in the number. What is
  8.  * my number? (See https://youtu.be/K6Qc4oK_HqY)
  9.  *
  10.  */
  11.  
  12. #include <stdio.h>
  13.  
  14. int main() {
  15.   char digits[10] = { 0 };
  16.   char count[10] = { 10, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  17.   int i, d;
  18.  
  19.   while (1) {
  20.     for (i=0; i<10; i++) if (count[i] != digits[i]) break;
  21.     if (i==10) {
  22.       for (i=0; i<10; i++) putchar('0'+digits[i]);
  23.       putchar('\n');
  24.     }
  25.     for (i=9; i>=0; i--) {
  26.       d = digits[i];
  27.       count[d]--;
  28.       if (++d == 10) d=0;
  29.       digits[i] = d;
  30.       count[d]++;
  31.       if (d) break;
  32.     }
  33.     if (i<0) break;
  34.   }
  35.   return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement