(大数乘小数、大数加小数、大数相乘、大数阶乘、大数进制转换)

#include <stdio.h>  
#include <string.h>  

//大数乘小数
void mult(char c[],int m,char t[])  
{  
    char s[100];  
    int len=strlen(c);  
    for(int i=0; i<len; i++)  
        s[len-i-1]=c[i]-'0';  
    int flag,add=0;  
    for(int i=0; i<len; i++)  
    {  
        int k=s[i]*m+add;  
        if(k>=10)  
        {  
            s[i]=k%10;  
            add=k/10;  
            flag=1;  
        }  
        else  
        {  
            s[i]=k;  
            add=0;  
            flag=0;  
        }  
    }  
    while(add)  
    {  
        s[len++]=add%10;  
        add/=10;  
    }  
    for(int i=0; i<len; i++)  
        t[len-1-i]=s[i]+'0';  
    t[len]='\0';  
}  
//大数加小数  
void addt(char a[],int b,char c[])  
{  
    int len=strlen(a);  
    char s[100];  
    for(int i=0; i<len; i++)  
        s[len-i-1]=a[i]-'0';  
    int add=0;  
    for(int i=0;; i++)  
    {  
        if(i>=len)  
        {  
            s[i]=0;  
            len++;  
        }  
        int k=s[i]+b%10+add;  
        b/=10;  
        if(k>=10)  
        {  
            s[i]=k%10;  
            add=k/10;  
        }  
        else  
        {  
            s[i]=k;  
            add=0;  
        }  
        if(b==0&&add==0)break;  
    }  
    for(int i=0; i<len; i++)  
        c[len-1-i]=s[i]+'0';  
    c[len]='\0';  

}  

int main()  
{  
    char c[1000],t[1000],sum[1000];  
    int m;  
    while(~scanf("%s%d",c,&m))  
    {  
        mult(c,m,t);  
        printf("%s\n",t);  
        addt(c,m,sum);  
        printf("%s\n",sum);  
    }  
    return 0;  
}  





//大数相乘
#include <stdio.h>  
#include <string.h>  

void mult(char a[],char b[],char s[])  
{  
   int i,j,k = 0,alen,blen,sum = 0,res[65][65]={0},flag = 0;  
   char result[65];  
   alen = strlen(a);  
   blen = strlen(b);  
   for(i = 0;i<alen;i++)  
   {  
       for(j = 0;j<blen;j++)  
       res[i][j] = (a[i]-'0')*(b[j]-'0');  
   }  
   for(i = alen-1;i>=0;i--)  
   {  
       for(j = blen-1;j>=0;j--)  
       {  
           sum = sum+res[i+blen-j-1][j];  
           printf("res = %d\n",res[i+blen-j-1][j]);  
       }  
       result[k] = sum%10;  
       k++;  
       sum = sum/10;  
   }  
   for(i = blen-2;i>=0;i--)  
   {  
       for(j = 0;j<=i;j++)  
       {  
           sum = sum+res[i-j][j];  
       }  
       result[k] = sum%10;  
       k++;  
       sum = sum/10;  
   }  
   if(sum)  
   {  
       result[k] = sum;  
       k++;  
   }  
   for(i = 0;i<k;i++)  
   result[i]+='0';  
   for(i = k-1;i>=0;i--)  
   s[i] = result[k-1-i];  
   s[k] = '\0';  
   while(1)  
   {  
       if(strlen(s)!=strlen(a) && s[0] == '0')  
       strcpy(s,s+1);  
       else  
       break;  
   }  
}  

int main()  
{  
    char c[1000],t[1000],sum[1000];  
    int m;  
    while(~scanf("%s%s",c,t))  
    {  
        mult(c,t,sum);  
        printf("%s\n",sum);  
    }  
    return 0;  
}  


//大数阶乘

#include<iostream>  
#include<cmath>  
using namespace std;  
int main()  
{  
    void factorial(int n,int *) ;  
    int b[10000];  
    int n;  
    while(cin>>n)  
    {  
        factorial(n,b);  
    }  
    return 0;  
}  
void factorial(int n,int b[10000])  
{  
    long a[10000];  
    int i,j,l,c,m=0,w;  
    a[0]=1;  
    for(i=1; i<=n; i++)  
    {  
        c=0;  
        for(j=0; j<=m; j++)  
        {  
            a[j]=a[j]*i+c;  
            c=a[j]/10000;  
            a[j]=a[j]%10000;  
        }  
        if(c>0)  
        {  
            m++;  
            a[m]=c;  
        }  
    }  
    w = m*4+log10(a[m])+1;  
    cout << w << endl;  
    cout<<a[m];  
    for(i=m-1; i>=0; i--)  
        cout<<a[i];  
    cout<<endl;  
}  



//进制转换


#include <stdio.h>  
#include <string.h>  

void consversion(char s[],char s2[],long d1,long d2)  
{  
    long i,j,t,num;  
    char c;  
    num = 0;  
    for(i = 0;s[i]!='\0';i++)  
    {  
        if(s[i]<='9' && s[i]>='0')  
        t = s[i] - '0';  
        else  
        t = s[i] - 'A' +10;  
        num = num*d1+t;  
    }  
    i = 0;  
    while(1)  
    {  
        t = num%d2;  
        if(t<=9)  
        s2[i] = t+'0';  
        else  
        s2[i] = t+'A'-10;  
        num/=d2;  
        if(num == 0)  
        break;  
        i++;  
    }  
    for(j = 0;j<i/2;j++)  
    {  
        c = s2[j];  
        s2[j] = s[i-j];  
        s2[i-j] = c;  
    }  
    s2[i+1]='\0';  
}  

int main()  
{  
    char s1[1000],s2[1000];  
    int d1,d2;  
    while(~scanf("%s%d%d",s1,&d1,&d2))  
    {  
        consversion(s1,s2,d1,d2);  
        printf("%s\n",s2);  
    }  

    return 0;  
}  



posted @ 2017-04-19 21:23  xushukui  阅读(240)  评论(0编辑  收藏  举报