viewing paste Unknown #15270 | C

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
 #include <stdio.h>
 #include <conio.h>
 
 
int self_desc(const char *s)
{
    unsigned char cnt[10] = {0};
    int i;
    for (i = 0; s[i] != '\0'; i++) cnt[s[i] - '0']++;
    for (i = 0; s[i] != '\0'; i++) if (cnt[i] + '0' != s[i]) return 0;
    return 1;
}
 
void gen(int n)
{
    char d[11];
    int one, i;
    /* one = 0 may be confusing.  'one' is the number of digit 1s */
    for (one = 0; one <= 2 && one < n - 2; one++) {
        for (i = 0; i <= n; d[i++] = 0);
 
        if ((d[0] = n - 2 - one) != 2) {
            d[2] = d[d[0] - 0] = 1;
            d[1] = 2;
        } else {
            d[1] = one ? 1 : 0;
            d[2] = 2;
        }
 
        for (i = 0; i < n; d[i++] += '0');
        if (self_desc(d)) printf("%s\n", d);
    
    }
}
 
int main()
{
    int i;
    const char *nums[] = { "1210", "1337", "2020", "21200", "3211000", "42101000", 0};
 
    for (i = 0; nums[i]; i++)
    printf("%s is %sself describing\n", nums[i],
    self_desc(nums[i]) ? "" : "not ");
 
//  printf("\nAll autobiograph numbers:\n");
//  for (i = 0; i < 11; i++)
   //  gen(1);
    
    
    getch();
    return 0;
}
 
Viewed 570 times, submitted by Guest.