SDUT 3792 Snow祝你元宵节快乐!

来源:互联网 发布:北京市java招聘 编辑:程序博客网 时间:2024/05/16 06:31

Snow祝你元宵节快乐!
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description

元宵节到了,Snow 也准备一掷千金买汤圆来庆祝元宵节。Snow 非常慷慨,他将分享给你一定数量的汤圆,那么是多少个汤圆呢?答案是 n!(n 的阶乘)…… 嘻嘻别傻了,Snow 要分享给你的是 n! 的位数个汤圆,但前提是你得求出 n! 有多少位哦。

Input

输入数据有多组(数据组数不超过 500),到 EOF 结束。

每组数据输入 n (1 <= n <= 500)。

Output

对于每组数据,输出一行,表示 n! 的位数。

Example Input

1
20

Example Output

1
19

代码:

#include<stdio.h>int main(){    int a[1000];    int len, i, carry, n, num, product, j;    while(~scanf("%d", &n))    {        len = 1;//有多少位        a[0] = 1;        num = 0;//长度        for(i = 1; i <= n; i++)        {            carry = 0;            for(j = 0; j < len; j++)            {                product = a[j] * i + carry;//计算当前位的乘积                a[j] = product % 1000;//进位后的值通过乘积取余1000得到                carry = product / 1000;//进位数通过乘积除以1000得到            }            a[j] = carry;            if(carry) len++;//如果carry不为0代表进位成功,长度加一        }        num = num + (len-1) * 3;//每位有三个数,最后一位的数还不确定        product = a[len-1];//求最后一位对应几位数        while(product > 0)        {            num++;            product = product / 10;        }        printf("%d\n", num);    }    return 0;}
0 0