大数阶乘

来源:互联网 发布:mac电脑怎么卸载软件 编辑:程序博客网 时间:2024/06/18 07:42

大数阶乘
时间限制: 3000ms内存限制: 128000KB64位整型: Java 类名:
上一题 提交 运行结果 统计 讨论版 下一题
类型:
没有

添加
题目描述
我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它?
输入
输入一个整数m(0

#include<stdio.h>#include<string.h>int a[20000];void gg(int n){    memset(a,0,sizeof(a));    a[0]=1;    int i,j;    for(i=2; i<=n; i++)    {        int c=0;        for(j=0; j<19001; j++)        {            int s=a[j]*i+c;            a[j]=s%10;            c=s/10;        }    }}int main(){    int n,i,f;    scanf("%d",&n);    gg(n);    for(i=19000;i>=0;i--)    {        if(a[i]!=0)        {            f=i;            break;        }    }    for(i=f;i>=0;i--)        printf(i==0?"%d\n":"%d",a[i]);    return 0;}

A+B Problem II
时间限制:3000 ms | 内存限制:65535 KB
难度:3
描述
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

A,B must be positive.

输入
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
输出
For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line is the an equation “A + B = Sum”, Sum means the result of A + B. Note there are some spaces int the equation.
样例输入
2
1 2
112233445566778899 998877665544332211
样例输出
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

#include<stdio.h>#include<string.h>int a2[1005],b2[1005],c[1005];void gg(char q[2000],int w[2000],int lon){    int i,n=0;    for(i=lon-1; i>=0; i--)        w[n++]=q[i]-'0';}void ff(int y[2000],int u[2000],int o[2009],int l){    int i;    for(i=0; i<l; i++)        o[i]=y[i]+u[i];    for(i=0; i<l; i++)    {        o[i+1]+=o[i]/10;        o[i]%=10;    }}int main(){    int n,an,la,lb,z,k,flag=0;    scanf("%d",&n);    an=n;    while(n--)    {        if(flag==1)            printf("\n");        char a1[1005],b1[1005];        scanf("%s%s",a1,b1);        memset(a2,0,sizeof(a2));        memset(b2,0,sizeof(b2));        memset(c,0,sizeof(c));        la=strlen(a1);        lb=strlen(b1);        gg(a1,a2,la);        gg(b1,b2,lb);        if(la>lb)            la^=lb^=la^=lb;        ff(a2,b2,c,lb);        printf("Case %d:\n",an-n);        printf("%s + %s = ",a1,b1);        int d;        if(c[lb])            d=lb;        else            d=lb-1;        for(int i=d; i>=0; i--)            printf(i==0?"%d\n":"%d",c[i]);        flag=1;    }    return 0;}
原创粉丝点击