zoukankan      html  css  js  c++  java
  • 题目1137:浮点数加法(高精度小数加法)

    题目链接:http://ac.jobdu.com/problem.php?pid=1137

    详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

    参考代码:

    //
    //  1137 浮点数加法.cpp
    //  Jobdu
    //
    //  Created by PengFei_Zheng on 16/04/2017.
    //  Copyright © 2017 PengFei_Zheng. All rights reserved.
    //
     
    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <string.h>
    #include <cmath>
    #define MAX_SIZE 1010
    using namespace std;
     
     
    struct fld{
        int p[MAX_SIZE];//整数部分(逆序存储)123. -->321.
        int q[MAX_SIZE];//小数部分(正序存储).456 -->.456
        int pSize;//整数长度
        int qSize;//小数长度
        void init(){//初始化操作
            memset(p,0,sizeof(p));
            memset(q,0,sizeof(q));
            pSize=0;
            qSize=0;
        }
        fld operator + (const fld &a)const{//重载加法运算符
            fld ret;//保存返回结果
            ret.init();
            int qLen = qSize > a.qSize ? qSize : a.qSize;//小数长度
            int carry = 0;//保存进位,以及小数到整数部分的进位
            ret.qSize = qLen;
            for(int i = qLen-1 ; i >= 0 ; i--){//小数部分相加
                int tmp = q[i]+a.q[i] + carry;
                ret.q[--qLen] = tmp%10;//保存结果
                carry = tmp/10;//更新进位
            }
            int pLen = pSize > a.pSize ? pSize : a.pSize;//整数长度
            for(int j = 0 ; j < pLen ; j++){//整数部分相加
                int tmp = p[j]+a.p[j] + carry;
                ret.p[ret.pSize++] = tmp%10;//保存结果
                carry = tmp/10;//更新进位
            }
            if(carry!=0){//当前进位不为0,进位并保存下来
                ret.p[ret.pSize++]=carry;
            }
            return ret;
        }
        void output(){
            int i = pSize - 1;
            while(p[i]==0 && i>=0 )i--;//移除高位的0
            if(i==-1){
                printf("0");
            }
            else{
                while(i>=0){//倒序输出整数部分
                    printf("%d",p[i--]);
                }
            }
            int j = qSize - 1;
            while(q[j]==0 && j>=0) j--;//移除小数部分低位0,并保存小数部分有效长度
            if(j!=-1){
                printf(".");//打印小数点
                int k = 0;
                while(k <= j){//输出小数部分
                    printf("%d",q[k++]);
                }
            }
            printf("
    ");
        }
    };
     
    fld change(char s[]){
        fld tmp;
        tmp.init();
        int p;
        for(p = 0; s[p] != '.'; p ++);//定位小数点下标
        int len = (int)strlen(s);
        for(int i = p+1 ; i < len ; i++){//正序存储小数部分
            tmp.q[tmp.qSize++] = s[i]-'0';
        }
        for(int j = p-1; j >= 0 ; j--){//逆序存储整数部分
            tmp.p[tmp.pSize++] = s[j]-'0';
        }
        return tmp;
    }
     
    int n;
    char A[MAX_SIZE],B[MAX_SIZE];
     
    int main(){
        while(scanf("%d",&n)!=EOF){
            while(n--){
                scanf("%s%s",A,B);
                fld a,b,c;
                a.init();
                b.init();
                c.init();
                a = change(A);
                b = change(B);
                c = a+b;//使用重载的加法运算符
                c.output();//打印结果
            }
        }
    }
    /**************************************************************
        Problem: 1137
        User: zpfbuaa
        Language: C++
        Result: Accepted
        Time:320 ms
        Memory:1520 kb
    ****************************************************************/
  • 相关阅读:
    004-DQN
    003-sarsa
    002-Q Leaning
    001-强化学习简介
    阿里云GPU服务器配置深度学习环境-远程访问-centos,cuda,cudnn,tensorflow,keras,jupyter notebook
    003-keras模型的保存于加载
    004-linux常用命令-网络命令
    004-linux常用命令-压缩解压命令
    004-linux常用命令-添加管理员用户
    004-linux常用命令-用户管理命令
  • 原文地址:https://www.cnblogs.com/zpfbuaa/p/6719293.html
Copyright © 2011-2022 走看看