题解 | #浮点数加法#

浮点数加法

https://www.nowcoder.com/practice/ddec753f446e4ba4944e35378ba635c8

#include<bits/stdc++.h>

using namespace std;

//取得数字
void GetNum(string a, string &aL, string &aR) {
    bool noDot = true;
    for (int i = 0; i < a.size(); ++i) {
        if (a[i] != '.' && noDot) {
            aL.push_back(a[i]);
        } else if (a[i] == '.') {
            noDot = false;
        } else {
            aR.push_back(a[i]);
        }
    }
}

//对阶、计算
void CaculateR(string a, string b, string &c, int &carry) {
    int len = 0;
    if (a.size() > b.size()) {
        len = a.size() - b.size();
        for (int i = 0; i < len; ++i) {
            b.push_back('0');
        }
    } else {
        len = b.size() - a.size();
        for (int i = 0; i < len; ++i) {
            a.push_back('0');
        }
    }
    for (int i = a.size() - 1; i >= 0; --i) {
        int temp = a[i] + b[i] - '0' - '0' + carry;
        carry = temp / 10;
        temp = temp % 10;
        c.insert(c.begin(),temp + '0') ;
    }

}

//对阶、计算
void CaculateL(string a, string b, string &c, int &carry) {
    for (int i = a.size() - 1, j = b.size() - 1; i >= 0 && j >= 0; --i, --j) {
        int temp = a[i] + b[j] - '0' - '0' + carry;
        carry = temp / 10;
        temp = temp % 10;
        c.insert(c.begin(),temp + '0') ;
    }
    if (a.size() > b.size() ) {
        for (int i = a.size() - b.size() - 1; i >= 0; --i) {
            int temp = a[i] - '0' + carry;
            carry = temp / 10;
            temp = temp % 10;
            c.insert(c.begin(),temp + '0') ;
        }
    } else {
        for (int i = b.size() - a.size() - 1; i >= 0; --i) {
            int temp = b[i] - '0' + carry;
            carry = temp / 10;
            temp = temp % 10;
            c.insert(c.begin(),temp + '0') ;
        }
    }

}

int main() {
    //输入
    string a;
    getline(cin, a);
    string b;
    getline(cin, b);
    //取得数字
    string aL;//a小数点左边的
    string aR;//a小数点右边的
    GetNum(a, aL, aR);
    string bL;//b小数点左边的
    string bR;//b小数点右边的
    GetNum(b, bL, bR);

    //计算
    int carry = 0;
//    char cR[max(aR.size(), bR.size())];
//    char cL[max(aL.size(), bL.size())];
//    string resR = cR;
//    string resL = cL;
    string resR;
    string resL;
    CaculateR(aR, bR, resR, carry);
    CaculateL(aL, bL, resL, carry);


    printf("%s.%s\n", resL.c_str(),resR.c_str());
    return 0;
}

全部评论

相关推荐

首先讲三个故事,关于牛客的事件一:2024年,牛客上有一对高学历情侣,求职方向与我当时一致,都是嵌入式方向。他们恰好是我的朋友,专业能力和学历背景都很扎实,也因此拿到了不少优质offer。和很多求职者一样,他们把offer情况整理后发在平台上,本意是记录与交流,但很快引发了争议。有声音指责他们“集邮”“不释放名额”,认为这种展示本身就是一种炫耀。最终讨论失控,当事人删除内容,事件也很快被遗忘。事件二:小红书评论区,一条评价获得了不少共鸣:“感觉牛客就是当年那群做题区毕业了开始找工作还收不住那股味,颇有一种从年级第一掉到年纪第二后抱怨考不上大学的味道”,这条评论被水印里这个同学转发到牛客后,评论...
小型域名服务器:当看到别人比自己强的时候,即便这是对方应得的,很多人会也下意识的歪曲解构对方的意图,来消解自己在这本就不存在的比较中输掉的自信,从而平白制造出很多无谓的争论。比如你会在空余时间来写优质好文,而我回家只会暗区突围,那么我就可以作为键盘侠在这里评论你是不是XXXXXXXX。即便我自己都知道这是假的,但只要这没那么容易证伪,那么当你开始回应的时候,脏水就已经泼出去了,后面可能会有更多的人带着情绪来给我点赞,而毫不关注你写的文章内容本身是啥了。
SAGIMA牛马咖啡
点赞 评论 收藏
分享
01-12 09:24
门头沟学院 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务