zoukankan      html  css  js  c++  java
  • Go--实现两个大数相乘

    -----

    import (
        "bufio"
        "fmt"
        "os"
        "strings"
    )
    
    func multi(str1, str2 string) (result string) {
    
        if len(str1) == 0 && len(str2) == 0 {
            result = "0"
            return
        }
    
        var index1 = len(str1) - 1
        var index2 = len(str2) - 1
        var left int
    
        for index1 >= 0 && index2 >= 0 {
            c1 := str1[index1] - '0'
            c2 := str2[index2] - '0'
    
            sum := int(c1) + int(c2) + left
            if sum >= 10 {
                left = 1
            } else {
                left = 0
            }
            c3 := (sum % 10) + '0'
            result = fmt.Sprintf("%c%s", c3, result)
            index1--
            index2--
        }
    
        for index1 >= 0 {
            c1 := str1[index1] - '0'
            sum := int(c1) + left
            if sum >= 10 {
                left = 1
            } else {
                left = 0
            }
            c3 := (sum % 10) + '0'
    
            result = fmt.Sprintf("%c%s", c3, result)
            index1--
        }
    
        for index2 >= 0 {
            c1 := str2[index2] - '0'
            sum := int(c1) + left
            if sum >= 10 {
                left = 1
            } else {
                left = 0
            }
            c3 := (sum % 10) + '0'
            result = fmt.Sprintf("%c%s", c3, result)
            index2--
        }
    
        if left == 1 {
            result = fmt.Sprintf("1%s", result)
        }
        return
    }
    
    func main() {
        reader := bufio.NewReader(os.Stdin)
        result, _, err := reader.ReadLine()
        if err != nil {
            fmt.Println("read from console err:", err)
            return
        }
    
        strSlice := strings.Split(string(result), "+")
        if len(strSlice) != 2 {
            fmt.Println("please input a+b")
            return
        }
    
        strNumber1 := strings.TrimSpace(strSlice[0])
        strNumber2 := strings.TrimSpace(strSlice[1])
        fmt.Println(multi(strNumber1, strNumber2))
    }
  • 相关阅读:
    leetcode312 戳气球
    leetcode1283 使结果不超过阈值的最小除数
    软件管理相关命令
    win10+Tensorflow2.1+anaconda python3.7安装
    ResNet残差网络(可以解决梯度消失)
    梯度消失&梯度爆炸(Vanishing/exploding gradients)
    高方差和高偏差
    tf.nn.conv1d
    tensorboard
    卷积
  • 原文地址:https://www.cnblogs.com/PasserByOne/p/12019885.html
Copyright © 2011-2022 走看看