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))
}
Go--实现两个大数相乘的更多相关文章
- 两个大数相乘-Java
两个字符串表示两个非常大的数,请设计算法计算这两个大数的乘积,结果用字符串表示.例如S1="7832974972840919321747983209327",S2="19 ...
- 两个大数相乘 - 高精度FFT
HDU 1402 A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- Karatsuba乘法--实现大数相乘
Karatsuba乘法 Karatsuba乘法是一种快速乘法.此算法在1960年由Anatolii Alexeevitch Karatsuba 提出,并于1962年得以发表.此算法主要用于两个大数相乘 ...
- 1051:A × B problem 大数相乘
给你两个整数,请你计算A × B. 输入 数据的第一行是整数T(1 ≤ T ≤ 20),代表测试数据的组数.接着有T组数据,每组数据只有一行,包括两个非负整数A和B.但A和B非常大,Redraimen ...
- 大数相乘算法C++版
#include <iostream> #include <cstring> using namespace std; #define null 0 #define MAXN ...
- java版大数相乘
在搞ACM的时候遇到大数相乘的问题,在网上找了一下,看到了一个c++版本的 http://blog.csdn.net/jianzhibeihang/article/details/4948267 用j ...
- linux c 实现大数相乘
#include <stdio.h> #include <string.h> #include <math.h> #include <stdbool.h& ...
- Linux C/C++ 编程练手 --- 大数相加和大数相乘
最近写了一个大数相乘和相加的程序,结果看起来是对的.不过期间的效率可能不是最好的,有些地方也是临时为了解决问题而直接写出来的. 可以大概说一下相乘和相加的解决思路(当然,大数操作基本就是两个字符串的操 ...
- Java 大数相乘、大数相加、大数相减
思路来源:: https://blog.csdn.net/lichong_87/article/details/6860329 /** * @date 2018/6/22 * @description ...
随机推荐
- c++-友元函数和友元类
友元函数 友元函数和友元类(破坏类的封装性) 面向对象编程思想 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include & ...
- System.InvalidOperationException: The binary operator NotEqual is not defined for the types 'Microsoft.EntityFrameworkCore.Storage.ValueBuffer' and 'Microsoft.EntityFrameworkCore.Storage.ValueBuffer'.
.netcore 2.1使用左表连接时报错,原因是对数据库中实体 DateTimeOffset date做查询判断时,将数据库中date转为string进行了判断,这样判断是错误的,并且效率低,应该是 ...
- Android 状态栏通知 Notification
private NotificationManager manager; private Notification.Builder builder; @Override protected void ...
- Android utils 工具类之MD5加密 MD5Utils
实际开发过程中,密码不会保存明文形式,容易被别人窃取. MD5加密是不可逆的加密算法,只能由明文转换成密文. 但是有些人通过将明文对应的密文保存在数据库中,进行查询破解密文.为防止这种解密,根据许多对 ...
- Android odex,oat文件的反编译,回编译
现在,许多Android手机的ROM包在生成过程中都启用优化,把jar文件抽空,生成odex/oat和vdex文件,以在运行时省掉编译时间.如果想对这些jar进行修改,就要修改它们所对应的odex或者 ...
- 如何在 Chrome中导出、导入书签和密码
目录 书签 密码 书签 1.导出 点击浏览器右上角的三小点,选择"书签",再选择"书签管理器",进入如下页面 点击蓝色书签栏右上角的三小点,选择"导出 ...
- Docker 私服Registry简介与使用Docker-Compose安装Registry
场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...
- tomcat修改使用指定的jdk版本
linux安装的jdk1.6,无法满足当前tomcat项目使用,所以需要指定为更高版本的jdk. 主要修改tomcat bin目录下的catalina.sh和setclasspath.sh文件 添加内 ...
- Nginx 入门学习
什么是 Nginx Nginx 是一款轻量级高性能的web 和 反向代理服务器,类似于Apache,也是一个 IMAP/POP3/SMTP (电子邮件)代理服务器.由俄罗斯程序设计师 Igor Sys ...
- js效果 整理
整理中... 1.js获取页面及元素高度.宽度 其他参考文献:http://www.open-open.com/lib/view/open1420120422531.html js: 网页可见区域宽: ...