码迷,mamicode.com
首页 > 其他好文 > 详细

模板 高精度计算

时间:2015-05-03 11:55:09      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:

【模板】高精度计算

 

大数相加:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 
 5 void add(char a[],char b[],char back[])
 6 {
 7     int i,j,k,up,x,y,z,l;
 8     char *c;
 9     if(strlen(a) > strlen(b))
10         l = strlen(a)+2;
11     else
12         l = strlen(b)+2;
13     c = (char*)malloc(l*sizeof(char));
14     i = strlen(a)-1;
15     j = strlen(b)-1;
16     k = 0;
17     up = 0;
18     while(j>=0 || i>=0)
19     {
20         if(i<0) x = 0;
21         else
22             x = a[i];
23         if(j<0) y = 0;
24         else
25             y = b[j];
26         z = x-0+y-0;
27         if(up)
28             z++;
29         if(z>9)
30         {
31             up = 1;
32             z%=10;
33         }
34         else
35             up = 0;
36         c[k++] = z+0;
37         i--;
38         j--;
39     }
40     if(up)
41         c[k++] = 1;
42     i = 0;
43     c[k] = \0;
44     for(k-=1; k>=0; k--)
45         back[i++] = c[k];
46     back[i] = \0;
47 }
48 
49 int main()
50 {
51     char c[10000],t[10000],sum[1000];
52     int m;
53     scanf("%d%*c",&m);
54     while(m--)
55     {
56         scanf("%s%s",c,t);
57         add(c,t,sum);
58         printf("%s\n",sum);
59         if(m)
60             printf("\n");
61     }
62     return 0;
63 }

 

大数加小数:

 1 void addt(char a[],int b,char c[])
 2 {
 3     int len=strlen(a);
 4     char s[100];
 5     for(int i=0; i<len; i++)
 6         s[len-i-1]=a[i]-0;
 7     int add=0;
 8     for(int i=0;; i++)
 9     {
10         if(i>=len)
11         {
12             s[i]=0;
13             len++;
14         }
15         int k=s[i]+b%10+add;
16         b/=10;
17         if(k>=10)
18         {
19             s[i]=k%10;
20             add=k/10;
21         }
22         else
23         {
24             s[i]=k;
25             add=0;
26         }
27         if(b==0&&add==0)break;
28     }
29     for(int i=0; i<len; i++)
30         c[len-1-i]=s[i]+0;
31     c[len]=\0;
32 
33 }
34 
35 int main()
36 {
37     char c[1000],t[1000],sum[1000];
38     int m;
39     while(~scanf("%s%d",c,&m))
40     {
41         mult(c,m,t);
42         printf("%s\n",t);
43         addt(c,m,sum);
44         printf("%s\n",sum);
45     }
46     return 0;
47 }

 

 

大数相乘:

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void mult(char a[],char b[],char s[])
 5 {
 6    int i,j,k = 0,alen,blen,sum = 0,res[65][65]={0},flag = 0;
 7    char result[65];
 8    alen = strlen(a);
 9    blen = strlen(b);
10    for(i = 0;i<alen;i++)
11    {
12        for(j = 0;j<blen;j++)
13        res[i][j] = (a[i]-0)*(b[j]-0);
14    }
15    for(i = alen-1;i>=0;i--)
16    {
17        for(j = blen-1;j>=0;j--)
18        {
19            sum = sum+res[i+blen-j-1][j];
20            printf("res = %d\n",res[i+blen-j-1][j]);
21        }
22        result[k] = sum%10;
23        k++;
24        sum = sum/10;
25    }
26    for(i = blen-2;i>=0;i--)
27    {
28        for(j = 0;j<=i;j++)
29        {
30            sum = sum+res[i-j][j];
31        }
32        result[k] = sum%10;
33        k++;
34        sum = sum/10;
35    }
36    if(sum)
37    {
38        result[k] = sum;
39        k++;
40    }
41    for(i = 0;i<k;i++)
42    result[i]+=0;
43    for(i = k-1;i>=0;i--)
44    s[i] = result[k-1-i];
45    s[k] = \0;
46    while(1)
47    {
48        if(strlen(s)!=strlen(a) && s[0] == 0)
49        strcpy(s,s+1);
50        else
51        break;
52    }
53 }
54 
55 int main()
56 {
57     char c[1000],t[1000],sum[1000];
58     int m;
59     while(~scanf("%s%s",c,t))
60     {
61         mult(c,t,sum);
62         printf("%s\n",sum);
63     }
64     return 0;
65 }

 

大数乘小数:

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void mult(char c[],int m,char t[])
 5 {
 6     char s[100];
 7     int len=strlen(c);
 8     for(int i=0; i<len; i++)
 9         s[len-i-1]=c[i]-0;
10     int flag,add=0;
11     for(int i=0; i<len; i++)
12     {
13         int k=s[i]*m+add;
14         if(k>=10)
15         {
16             s[i]=k%10;
17             add=k/10;
18             flag=1;
19         }
20         else
21         {
22             s[i]=k;
23             add=0;
24             flag=0;
25         }
26     }
27     while(add)
28     {
29         s[len++]=add%10;
30         add/=10;
31     }
32     for(int i=0; i<len; i++)
33         t[len-1-i]=s[i]+0;
34     t[len]=\0;
35 }

 

 

大数相乘:

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void sub(char s1[],char s2[],char t[])
 5 {
 6     int i,l1,l2,k;
 7     l2 = strlen(s2);
 8     l1 = strlen(s1);
 9     t[l1] = \0;
10     l1--;
11     for(i = l2-1; i>=0; i--,l1--)
12     {
13         if(s1[l1]-s2[i]>=0)
14             t[l1] = s1[l1] - s2[i] + 0;
15         else
16         {
17             t[l1] = 10+s1[l1] - s2[i]+0;
18             s1[l1-1]= s1[l1-1] -1;
19         }
20     }
21     k = l1;
22     while(s1[k]<0)
23     {
24         s1[k]+=10;
25         s1[k-1]-=1;
26         k--;
27     }
28     while(l1>=0)
29     {
30         t[l1] = s1[l1];
31         l1--;
32     }
33 loop:
34     if(t[0] == 0)
35     {
36         l1 = strlen(s1);
37         for(i = 0; i<l1-1; i++)
38             t[i] = t[i+1];
39         t[l1-1] = \0;
40         goto loop;
41     }
42     if(strlen(t) == 0)
43     {
44         t[0] = 0;
45         t[1] = \0;
46     }
47 }
48 
49 int main()
50 {
51     char c[1000],t[1000],sum[1000];
52     int m;
53     while(~scanf("%s%s",c,t))
54     {
55         sub(c,t,sum);
56         printf("%s\n",sum);
57     }
58     return 0;
59 }

 

大数阶乘:

 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 int main()
 5 {
 6     void factorial(int n,int *) ;
 7     int b[10000];
 8     int n;
 9     while(cin>>n)
10     {
11         factorial(n,b);
12     }
13     return 0;
14 }
15 void factorial(int n,int b[10000])
16 {
17     long a[10000];
18     int i,j,l,c,m=0,w;
19     a[0]=1;
20     for(i=1; i<=n; i++)
21     {
22         c=0;
23         for(j=0; j<=m; j++)
24         {
25             a[j]=a[j]*i+c;
26             c=a[j]/10000;
27             a[j]=a[j]%10000;
28         }
29         if(c>0)
30         {
31             m++;
32             a[m]=c;
33         }
34     }
35     w = m*4+log10(a[m])+1;
36     cout << w << endl;
37     cout<<a[m];
38     for(i=m-1; i>=0; i--)
39         cout<<a[i];
40     cout<<endl;
41 }

 

大数进制转换:

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void consversion(char s[],char s2[],long d1,long d2)
 5 {
 6     long i,j,t,num;
 7     char c;
 8     num = 0;
 9     for(i = 0;s[i]!=\0;i++)
10     {
11         if(s[i]<=9 && s[i]>=0)
12         t = s[i] - 0;
13         else
14         t = s[i] - A +10;
15         num = num*d1+t;
16     }
17     i = 0;
18     while(1)
19     {
20         t = num%d2;
21         if(t<=9)
22         s2[i] = t+0;
23         else
24         s2[i] = t+A-10;
25         num/=d2;
26         if(num == 0)
27         break;
28         i++;
29     }
30     for(j = 0;j<i/2;j++)
31     {
32         c = s2[j];
33         s2[j] = s[i-j];
34         s2[i-j] = c;
35     }
36     s2[i+1]=\0;
37 }
38 
39 int main()
40 {
41     char s1[1000],s2[1000];
42     int d1,d2;
43     while(~scanf("%s%d%d",s1,&d1,&d2))
44     {
45         consversion(s1,s2,d1,d2);
46 //将d1进制的s1转换成d2进制s2.
47         printf("%s\n",s2);
48     }
49 
50     return 0;
51 }

 

模板 高精度计算

标签:

原文地址:http://www.cnblogs.com/jeff-wgc/p/4473388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!