您的位置:首页 > 其它

UVa 623 500!

2014-11-03 17:40 363 查看



500!

In these days you can more and more often happen to see programs which perform some useful calculations being executed rather then trivial screen savers. Some of them check the system message queue and in case
of finding it empty (for examples somebody is editing a file and stays idle for some time) execute its own algorithm.
As an examples we can give programs which calculate primary numbers.

One can also imagine a program which calculates a factorial of given numbers. In this case it is the time complexity of order O(n) which makes troubles, but the memory requirements. Considering the fact that 500! gives 1135-digit number no
standard, neither integer nor floating, data type is applicable here.

Your task is to write a programs which calculates a factorial of a given number.
Assumptions: Value of a number ``n" which factorial should be calculated of does not exceed 1000 (although 500! is the name of the problem, 500! is a small limit).

Input

Any number of lines, each containing value ``n" for which you should provide value of n!

Output

2 lines for each input case. First should contain value ``n" followed by character `!'.
The second should contain calculated value n!.

Sample Input

10
30
50
100


Sample Output

10!
3628800
30!
265252859812191058636308480000000
50!
30414093201713378043612608166064768844377641568960512000000000000
100!
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000


第一次做这种OJ题,在经过无数次WA和无数次CE以及一次TL之后,终于AC了


<span style="font-size:14px;">#include <stdio.h>
#include <string.h>

#define MAX_LENGTH 3000

/**
 *输入不超过1000的正整数,输出n!的精确结果
 */
void Factorial();

int main(int argc, const char * argv[]) {

    Factorial();
    return 0;
}

void Factorial()
{
    int n;
    while (scanf("%d", &n) == 1) {
        int i, j = 1, len = 1;    /*初始化len = 1,每次手工模拟乘法后j为长度,所以在此处让j = 1,在循环体内让len = j便于循环*/
        int result[MAX_LENGTH];
        memset(result, 0, sizeof(result));
        
        result[0] = 1;
        
        for (i = 2; i <= n; i++) {
            int s = 0;
            int carry = 0;
            len = j;
            /*手工模拟乘法*/
            for (j = 0; j < len; j++) {
                s = result[j] * i + carry;
                result[j] = s % 10;
                carry = s / 10;
                if (len == j + 1 && carry) {
                    ++len;
                }
            }
            
        }
        
        /*清除前导0*/
        for (j = len - 1; j >= 0; j--) {
            if (result[j] != 0) {
                break;
            }
        }
        
        printf("%d!\n", n);
        
        if (n == 0) {
            printf("1");
        } else {
            for (i = j; i >= 0; i--) {
                printf("%d", result[i]);
            }
        }

        
        printf("\n");

    }

}
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: