hdu1753

1001.

题目来源:http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1003&cid=31373&problem=Problem%20%20C

解法或类型:循环结构。

Description

话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”。
这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加法。

现在,给你两个正的小数A和B,你的任务是代表大明计算出A+B的值。

Input

本题目包含多组测试数据,请处理到文件结束。
每一组测试数据在一行里面包含两个长度不大于400的正小数A和B。

Output

请在一行里面输出输出A+B的值,请输出最简形式。详细要求请见Sample Output。  

Sample Input

1.1 2.9

1.1111111111 2.3444323343

1 1.1

Sample Output

4

3.4555434454

2.1

题意描述
      给两个小数计算两者之和。
解题思路
      这个和上周的那道大数类似,更负责一些,以字符串输入,然后把整数部分小数部分存入数组中,定义个可存1000个数的数组,前500个存整数,后500存小数,这样就可以按整数的相加计算了(整数倒存,小数正存),然后输出的前500个,中间输出一个小数点,在输出下标从501开始的。
 
时空分析: 

  

程序代码:

#include<stdio.h>

#include<string.h>

int main()

{

       charstr1[500],str2[500];

       int a[1000],b[1000],c[1000],i,j,t,y,y1,v1,u,v,t1,t2,l1,l2,k1,k2,d,f,q,g;

      

       while(scanf("%s%s",str1,str2)!=EOF)

       {

              l1=strlen(str1);

              l2=strlen(str2);

              for(i=0;i<1000;i++)

              {

                     a[i]=0;

                     b[i]=0;

                     c[i]=0;

              }

              k1=0;

              for(i=0;str1[i]!='\0';i++)

              {

                     if(str1[i]=='.')

                     k1=i;

              }

              if(k1==0)

              {

                     g=0;

                     for(i=0;str1[i]!='\0';i++)

                     {

                            if(str1[i]=='0')

                            g++;

                            else

                            break;

                     }

                     //printf("%d\n",g);

                     t=500;

                     for(i=l1-1;i>=g;i--)

                     {

                            a[t]=str1[i]-'0';

                            t--;

                     }

                     t=t+1;

                     y=500;

              }

              else

              {

                     y1=500+l1-k1-1;

                     y=y1;

                     for(j=l1-1;str1[j]!='.';j--)

                     {

                            a[y1]=str1[j]-'0';

                            y1--;

                     }

                     g=0;

                     for(i=0;str1[i]!='\0';i++)

                     {

                            if(str1[i]=='0')

                            g++;

                            else

                            break;

                     }

                     t=500;

                     for(i=j-1;i>=g;i--)

                     {

                            a[t]=str1[i]-'0';

                            t--;

                     }

                     t=t+1;

              }

              k2=0;

              for(i=0;str2[i]!='\0';i++)

              {

                     if(str2[i]=='.')

                     k2=i;

              }

              if(k2==0)

              {

                     g=0;

                     for(i=0;str2[i]!='\0';i++)

                     {

                            if(str2[i]=='0')

                            g++;

                            else

                            break;

                     }

                     u=500;

                     for(i=l2-1;i>=g;i--)

                     {

                            b[u]=str2[i]-'0';

                            u--;

                     }

                     u=u+1;

                     v=500;

              }

              else

              {

                     v1=500+l2-k2-1;

                     v=v1;

                     for(j=l2-1;str2[j]!='.';j--)

                     {

                            b[v1]=str2[j]-'0';

                            v1--;

                     }

                    

                     g=0;

                     for(i=0;str2[i]!='\0';i++)

                     {

                            if(str2[i]=='0')

                            g++;

                            else

                            break;

                     }

                      

                     u=500;

                     for(i=j-1;i>=g;i--)

                     {

                            b[u]=str2[i]-'0';

                            u--;

                     }

                     u=u+1;

              }

              if(y>v)

              {

                     t1=y;y=v;v=t1;//v是最小的;

              }

              if(t>u)

              {

                     t1=t;t=u;u=t1;

              }

             

              for(i=v;i>=t;i--)

                     {

                            c[i-1]=0;

                            c[i]=c[i]+a[i]+b[i];

                            if(c[i]>9)

                            {

                                   c[i]=c[i]-10;

                                   c[i-1]=c[i-1]+1;;

                            }

                     }

                     d=0;

                     for(i=501;i<=v;i++)

                     {

                            if(c[i]==0)

                            d++;

                     }

                     f=0;

                     if(d==v-500)

                     {

                            f=1;

                     }

                    

                     if(c[t-1]!=0)

                     {

                            d=0;

                            for(i=t-1;i<=500;i++)

                            {

                                   if(c[i]==0)

                                   d++;

                            }

                            q=0;

                            if(d==500-t+2)

                            {

                                   q=1;

                            }

                            if(q==1)

                            printf("0");

                            else

                            {

                           

                            for(i=t-1;i<=500;i++)

                            {

                                   printf("%d",c[i]);

                            }

                            }

                            if(f==0&&(k1!=0||k2!=0))

                            {

                            printf(".");

                            g=0;

                            for(j=v;j>=i;j--)

                            {

                                   if(c[j]==0)

                                   g++;

                                   else

                                   break;

                            }

                            for(j=i;j<=v-g;j++)

                            {

                                   printf("%d",c[j]);

                            }

                            }

                            printf("\n");

                     }

                     if(c[t-1]==0)

                     {

                            d=0;

                            for(i=t;i<=500;i++)

                            {

                                   //printf("%d\n",c[i]);

                                   if(c[i]==0)

                                   d++;

                            }

                            q=0;

                            if(d==500-t+1)

                            {

                                   q=1;

                            }

                            if(q==1)

                            printf("0");

                            else

                            {

                            for(i=t;i<=500;i++)

                            {

                                   printf("%d",c[i]);

                            }

                            }

                            if(f==0&&(k1!=0||k2!=0))

                            {

                            printf(".");

                            g=0;

                            for(j=v;j>=i;j--)

                            {

                                   if(c[j]==0)

                                   g++;

                                   else

                                   break;

                            }

                            for(j=i;j<=v-g;j++)

                            {

                                   printf("%d",c[j]);

                            }

                            }

                            printf("\n");

                     }

       }

        

       return 0;

 }

错误分析:

 1、计算只要存对没啥问题,关键是输出的最简形式,最后结果需要去掉前面无用的0,和后面无用的0;还有如果是两个整数输出不能有小数点;这些特殊情况开始没有考虑到。

2、

3、

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值