zoukankan      html  css  js  c++  java
  • 大正小数加法(hdu1753)

    F - 大明A+B

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

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

    Input本题目包含多组测试数据,请处理到文件结束。
    每一组测试数据在一行里面包含两个长度不大于400的正小数A和B。Output请在一行里面输出输出A+B的值,请输出最简形式。详细要求请见Sample Output。
    Sample Input
    1.1 2.9
    1.1111111111 2.34443233434
    1 1.1
    21.3423 2.13423
    23432.32 1232
    222 232
    19.2 12.1
    19.2 12.9
    2013.2013 3029.3029
    99.9 99.9
    0.99 99.9
    99.9 0.1
    99.99 009.11
    00098.8 3.32
    1.2 1.9
    0.1 0.9
    1.0 0.10
    1.0001 0010.00100
    Sample Output
    • 4
    • 3.45554344544
    • 2.1
    • 23.47653
    • 24664.32
    • 454
    • 31.3
    • 32.1
    • 5042.5042
    • 199.8
    • 100.89
    • 100
    • 109.1
    • 102.12
    • 3.1
    • 1
    • 1.1
    • 11.0011
       
       
      #include<iostream>
      #include <string.h>
      #include <algorithm>
      #include <string>
      using namespace std;
      bool flag = false; // 标记小数加法是否需要向整数部分进位
      string cnt , ans ,  sum ;//cnt存整数部分 , ans存小数部分,sum为整体
      int index , indey ;//分别标记前置零和后置零的下标 
      
      string minplu(string a , string b)//小数部分加法
      {
          string f ;
          char e ;
          int k = 0  ;
          for(int i = b.length() - 1 ; i >= 0 ; i--)//a为小数长度更长,用更长的小数部分进行操作
          {
              e = a[i] ;//防止进位计算覆盖
              a[i] = ((a[i] - '0') + (b[i] - '0') + k) % 10 + '0' ;
              k = ((e - '0') + (b[i] - '0') + k) / 10 ;
          }
          if(k) //判断是否需要向整数部分进位
              flag = true ;
          return a ;
      
      }
      string plu(string a , string b)//大数加法
      {
          string f ;
          int i = i = a.length() - 1, j , k = 0 , x , y ;
          if(flag)
          {
              a[i] ++ ; //将小数的进位加上
          }
          for( j = b.length() - 1 ; i >= 0 && j >= 0 ; i-- , j--)
          {
              x = a[i] - '0' ;
              y = b[j] - '0' ;
              f += (x + y + k ) % 10 + '0' ;
              k = (x + y + k ) / 10 ;
          }
          while(i >= 0)
          {
              x = a[i] - '0' ;
              f += (x + k) % 10 + '0' ;
              k = (x + k) / 10;
              i-- ;
          }
          while(j >= 0)
          {
              y = b[j] - '0';
              f += (y + k) % 10 + '0' ;
              k = (y + k) / 10 ;
              j -- ;
          }
          if(k)
              f += k + '0' ;
          reverse(f.begin() , f.end());
          return f ;
      }
      
      string cntclear(string a)//去整数部分前置零
      {
          index = a.find_first_not_of('0');
          if(index != a.npos)
              a.erase(0,index);
          else
              a = '0';
          return a ;
      }
      string ansclear(string a)//去小数部分后置零
      {
          reverse(a.begin() , a.end());
          indey = a.find_first_not_of('0');
          if(indey != a.npos)
              a.erase(0,indey);
          else
              a = "" ;
          reverse(a.begin() , a.end());
          return a ;
      }
      
      int main()
      {
          string a , b ;
          while(cin >> a >> b)
          {
              cnt = ans =  sum = "";
              flag = false ;
              int positiona = a.find('.') , positionb = b.find('.');
              if(positiona != a.npos && positionb != b.npos) // 如果a,b都为小数。。
              {
                  string c(a , positiona+1) , d(b , positionb+1) ;//将小数和整数分离
                  string e(a ,0 , positiona) , f(b ,0 , positionb) ;
                  if(c.length() > d.length())//小数相加
                      ans += minplu(c , d);
                  else
                      ans += minplu(d , c);
                  cnt += plu(e , f);//整数部分相加
      
                  cnt = cntclear(cnt);//整数部分去前置零
                  ans = ansclear(ans);//小数部分取后置零
      
                  if(ans == "")//判断小数部分是否为‘0’
                  cout << cnt << endl ;
                  else
                      cout << cnt + '.' + ans  << endl ;
      
              }
              else if(positiona != a.npos)//如果a为小数,b为整数
              {
                  string c(a , positiona+1) ;//将小数分离
                  string e(a ,0 , positiona) ;
                  ans += c ;
                  cnt +=plu(e , b);
                  sum = cnt + '.' + ans ;
                  cnt = cntclear(cnt);
                  ans = ansclear(ans);
                  if(ans == "")
                  cout << cnt << endl ;
                  else
                      cout << cnt + '.' + ans  << endl ;
              }
              else if(positionb != b.npos)//如果a为整数,b为小数
              {
                  string  d(b , positionb+1) ;//将小数分离
                  string  f(b ,0 , positionb) ;
                  ans += d ;
                  cnt += plu(f , a);
                  cnt = cntclear(cnt);
                  ans = ansclear(ans);
                  if(ans == "")
                  cout << cnt << endl ;
                  else
                      cout << cnt + '.' + ans  << endl ;
      
              }
              else
              {
                  cout << plu(a , b) << endl ;
      
              }
      
          }
      
          return 0;
      }

  • 相关阅读:
    Tensor总结
    Tensorflow池化
    conda操作
    KS值计算
    supervisor实践
    npm/yarn实践
    nni 环境搭建
    阿里云个人邮箱配置
    Jinja2宏使用
    利用VS code 远程调试 docker 中的 dotnet 应用
  • 原文地址:https://www.cnblogs.com/nonames/p/11200103.html
Copyright © 2011-2022 走看看