zoukankan      html  css  js  c++  java
  • 【a101】高精度实数加法

    Time Limit: 1 second
    Memory Limit: 2 MB

    问题描述
    给出两个高精度正实数(可以含有小数点或没有),最长200位,字符串读入 求它们的和,小数部分末尾的0要舍去。

    Input

    文件输入两行,第一行是被加数,第二行是加数,回车结束输入。

    Output

    输出求和的值,最后用回车结束
    Sample Input

    123456789.123456
    987654321.4321
    Sample Output

    1111111110.555556

    【题目链接】:http://noi.qz5z.com/viewtask.asp?id=a101

    【题解】

    把两个数字小数部分的长度变成一样(不足补零);
    这样小数部分就对齐了;
    然后把小数点去掉;
    进行正常的整数高精度加法就好了;
    然后在进位的时候先不要把个位的0去掉;
    进行完整数高精度加法之后;把小数点加到整数里面去;
    然后再去掉个位的0;
    要注意整数的情况比如0.9+0.1最后不要输成”1.”要把小数点去掉的。
    特判一下就好;
    去掉前导的0的话时候要注意别把小数点前的0也删掉了;
    不然0.5就会变成.5;

    【完整代码】

    #include <string>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    
    const int MAXN = 250;
    
    string s1,s2;
    int len1,len2;
    int a[MAXN],b[MAXN],c[MAXN]={0};
    
    void add0(string &s,int &l,int &r)
    {
        for (int i = l+1;i <= r;i++)
                    s+='0';
        l = r;
    }
    
    string sub(string s1,string s2)
    {
        int len1 = s1.size(),len2 = s2.size();
        reverse(s1.begin(),s1.end());
        reverse(s2.begin(),s2.end());
        for (int i = 1;i <= len1;i++)
            a[i] = s1[i-1]-'0';
        for (int i = 1;i <= len2;i++)
            b[i] = s2[i-1]-'0';
        for (int i = 1;i <= 210;i++)
            c[i] = a[i]+b[i];
        for (int i = 1;i <= 210;i++)
        {
            c[i+1] = c[i+1]+c[i]/10;
            c[i] %= 10;
        }
        string temp = "";
        for (int i = 1;i <= 210;i++)
        {
            char key = c[i]+'0';
            temp+=key;
        }
        return temp;
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        cin >> s1>>s2;
        len1 = s1.size(),len2 = s2.size();
        int pos1,pos2;
        pos1 = s1.find('.',0),pos2 = s2.find('.',0);
        int lp1,lp2;
        lp1 = len1-1-pos1,lp2 = len2-1-pos2;
        if (pos1!=-1 && pos2!=-1)
        {
            if (lp1>lp2)
                add0(s2,lp2,lp1);
            else
                add0(s1,lp1,lp2);
            s1.erase(s1.find('.',0),1);
            s2.erase(s2.find('.',0),1);
        }
        else
            if (pos1!=-1)
            {
                lp2 = 0;
                add0(s2,lp2,lp1);
                s1.erase(s1.find('.',0),1);
            }
            else
                if (pos2!=-1)
                {
                    lp1 = 0;
                    add0(s1,lp1,lp2);
                    s2.erase(s2.find('.',0),1);
                }
                else
                    lp1 = lp2 = 0;
        string tempans = sub(s1,s2);
        if (lp1!=0)
            tempans.insert(lp1,".");
        while (tempans!=""&& tempans[0]=='0')
            tempans.erase(0,1);
        if (tempans=="")
            puts("0");
        else
        {
            if (tempans[0]=='.')
                tempans.erase(0,1);
            reverse(tempans.begin(),tempans.end());
            int len = tempans.size();
            while (tempans[0]=='0' && tempans[1]!='.')
                tempans.erase(0,1);
            cout << tempans;
        }
        return 0;
    }
    
  • 相关阅读:
    (转)CentOS 和 Ubuntu 下的网络配置
    love 的Python 表示
    python mysqlLdb ImportError: DLL load failed: 找不到指定的模块
    elasticsearch7.11.1安装及使用小记
    python多进程代码示例
    在c++项目中使用高性能的rapidjson作为json处理库
    使用kenlm进行文本纠错
    供應商主檔建立流程
    SAP系統自帶范例
    内部订单作业流程
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626959.html
Copyright © 2011-2022 走看看