入门经典 第七章 7.7.1 除法

December 14, 2012

这道题目还是卡了很久,原因是要考虑0是不是出现过,要标记。


#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

void f(int a[], int n)
{
    a[n%10] = 1;
    n/=10;
    while (n)
    {
        a[n%10] = 1;
        n/=10;
    }
    return;
}

int main(void)
{
    int n, i, a[11], k, temp;
    freopen("in", "r", stdin);
    while (~scanf("%d", &n))
    {
        for (i = 100; i < 50000; ++i)
        {
            k = i * n;
            memset(a, 0, sizeof(a));
            temp = k;
            if (temp/100000!=0) continue;
            f(a, temp);
            f(a, i);
            a[temp/10000] = a[i/10000] = 1;
            int mrk=1;
            for (int s = 0; s < 10; ++s)
            {
                if(!a[s])
                {mrk = 0; break;}
            }
            if (mrk == 1) printf("%05d / %05d = %d\n", k, i, n);
        }
        
    }

    return 0;
}

标记的时候要认真考虑特殊情况。

comments powered by Disqus