2011年6月25日 星期六

ACM 568 Just the Facts

這題用10212 The Last Non-zero Digit的解法就可以完成了,做法更簡單。


#include <stdio.h>

int get10(int num)
{
    int count;
   
    count = 0;
    while (num >= 5)
    {
        count = count + num/5;
        num /= 5;
    }
   
    return count;
}

int main(void)
{
    int n, i, rs, tmp, two;
   
    while (scanf("%d", &n) == 1)
    {
        two = get10(n);
        rs = 1;
        for (i = 1; i <= n; i++)
        {
            tmp = i;
            while (tmp%5 == 0)
                tmp /= 5;
            while (two > 0 && tmp%2 == 0)
            {
                tmp /= 2;
                two--;
            }
            rs = rs * tmp%10;
        }
       
        printf("%5d -> %d\n", n, rs);
    }

    return 0;
}

沒有留言:

張貼留言