HDU 1753 大明A+B

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1753

小数的高精度运算

思路 把小数点前后分开存,然后统一进位,输出时再输出小数点。

View Code
  1 #include <stdio.h>
  2 #include <string.h>
  3 #define N 405
  4 int a[N];
  5 int b[N];
  6 int c[N];
  7 int a1[N];
  8 int b1[N];
  9 int c1[N];
 10 int d[N];
 11 char stra[1000];
 12 char strb[1000];
 13 int max2(int q,int p)
 14 {
 15     return q>p?q:p;
 16 }
 17 int main()
 18 {
 19     int i,j;
 20     while (scanf("%s %s",stra,strb)!=EOF)
 21     {
 22         int alen = strlen (stra);
 23         int flaga=0,flagb=0;
 24         for (i=0;i<alen;i++)
 25         {
 26             if (stra[i] == '.')
 27             {
 28                 flaga = i;
 29                 break;
 30             }
 31         }
 32         if (i == alen)
 33         {
 34             flaga = alen;
 35         }
 36         int aplen ;
 37         int blen = strlen(strb);
 38         for (i=0;i<blen;i++)
 39         {
 40             if (strb[i] == '.')
 41             {
 42                 flagb = i;
 43                 break;
 44             }
 45         }
 46         if (i == blen)
 47         {
 48             flagb = blen;
 49         }
 50         int bplen ;
 51         memset (a,0,sizeof(a));
 52         memset (b,0,sizeof(b));
 53         memset (c,0,sizeof(c));
 54         memset (a1,0,sizeof(a1));
 55         memset (b1,0,sizeof(b1));
 56         memset (c1,0,sizeof(c1));
 57         j=0;
 58         for (i=flaga-1;i>=0;i--)
 59         {
 60             a[j] = stra[i]-'0';
 61             j++;
 62         }
 63         int aclen = j--;
 64         j=0;
 65         for (i=flagb-1;i>=0;i--)
 66         {
 67             b[j] = strb[i]-'0';
 68             j++;
 69         }
 70         int bclen = j--;
 71         j=1;
 72         for (i=flaga+1;i<alen;i++)
 73         {
 74             a1[j] = stra[i]-'0';
 75             j++;
 76         }
 77         aplen = j--;
 78         j=1;
 79         for (i=flagb+1;i<blen;i++)
 80         {
 81             b1[j] = strb[i]-'0';
 82             j++;
 83         }
 84         bplen = j--;
 85         int maxplen = max2(aplen,bplen);
 86         for (i=maxplen;i>=1;i--)
 87         {
 88             c1[i-1] = (a1[i]+b1[i]+c1[i])/10;
 89             c1[i] = (a1[i]+b1[i]+c1[i])%10;
 90 
 91         }
 92         int maxclen = max2(aclen,bclen);
 93         for (i=0;i<maxclen;i++)
 94         {
 95             c[i+1] = (a[i]+b[i]+c[i])/10;
 96             c[i] = (a[i]+b[i]+c[i])%10;
 97         }
 98         if (c1[0])
 99         {
100             memset(d,0,sizeof(d));
101             d[0] = 1;
102             for (i=0;i<maxclen;i++)
103             {
104                 c[i+1] += (c[i]+d[i])/10;
105                 c[i] = (c[i]+d[i])%10;
106             }
107         }
108         int flag = 0;
109         for (i=maxclen+1;i>=0;i--)
110         {
111             if(c[i]!=0)
112                 flag = 1;
113             if(flag)
114                 printf("%d",c[i]);
115         }
116         if(flag == 0)
117             printf("0");
118         flag = 0;
119         for (i=1;i<=maxplen;i++)
120         {
121             if(c1[i]!=0)
122                 flag = i;
123         }
124         if(flag)
125         {
126             printf(".");
127             for (i=1;i<=flag;i++)
128                 printf("%d",c1[i]);
129         }
130         printf("\n");
131     }
132     return 0;
133 }
posted @ 2012-08-03 18:44  时光旅行的懒猫  阅读(282)  评论(0编辑  收藏  举报