Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Brute force solution to the self-descriptive number puzzle:
- *
- * I have a ten digit number. The first digit tells me how many zeros are
- * in the number. The second digit tells me how many ones are in the number.
- * The third digit tells me how many twos are in the number, and so on until
- * the tenth digit which tells me how many nines are in the number. What is
- * my number? (See https://youtu.be/K6Qc4oK_HqY)
- *
- */
- #include <stdio.h>
- int main() {
- char digits[10] = { 0 };
- char count[10] = { 10, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- int i, d;
- while (1) {
- for (i=0; i<10; i++) if (count[i] != digits[i]) break;
- if (i==10) {
- for (i=0; i<10; i++) putchar('0'+digits[i]);
- putchar('\n');
- }
- for (i=9; i>=0; i--) {
- d = digits[i];
- count[d]--;
- if (++d == 10) d=0;
- digits[i] = d;
- count[d]++;
- if (d) break;
- }
- if (i<0) break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement