当前位置: 首页 > news >正文

Acwing算法基础课--高精度加减乘除

AcWing 791. 高精度加法

代码部分(Python3)

def add(x,y):if len(x) < len(y): return add(y,x)x = x[::-1]y = y[::-1]t = 0res = []for i in range(len(x)):t += x[i]if i < len(y):t += y[i]res.append(t %10)t //= 10if t != 0: res.append(t)return res[::-1]if __name__ == "__main__":x = list(map(int,input()))y = list(map(int,input()))res = add(x,y)print("".join(map(str,res)))

AcWing 792. 高精度减法

代码部分(Python3)

def cmp(x,y):if len(x) != len(y): return len(x) > len(y)for i, j in zip(x,y):if i != j: return i > jreturn Truedef sub(x,y):x = x[::-1]y = y[::-1]t = 0res = []for i in range(len(x)):t = x[i] - tif i < len(y):t -= y[i]res.append(t %10)if t < 0: t = 1else: t = 0while len(res) > 1 and res[-1] == 0: res.pop()return res[::-1]if __name__ == "__main__":x = list(map(int,input()))y = list(map(int,input()))if cmp(x,y):res = sub(x,y)print("".join(map(str,res)))else:res = sub(y,x)print("-"+"".join(map(str,res)))

AcWing 793. 高精度乘法

代码部分(Python3)

def mul(x,y):res = []r = 0x = x[::-1]for i in range(len(x)):c = x[i] * y + rres.append(c % 10)r = c//10if r != 0: res.append(r)while len(res) > 1 and res[-1] == 0: res.pop()return res[::-1]if __name__ == "__main__":x = list(map(int,input()))y = int(input())c = mul(x,y)print("".join(map(str,c)))

AcWing 794. 高精度除法

代码部分(Python3)

def div(x,y):r = 0res = []for i in range(len(x)):c = r * 10 + x[i]res.append(c // y)r = c % yres = res[::-1]while len(res) > 1 and res[-1] == 0: res.pop()return res[::-1], rif __name__ == "__main__":x = list(map(int,input()))y = int(input())res, r = div(x,y)print("".join(map(str,res)))print(r)

规律总结

  • 加法,减法,乘法要对输入值逆序
  • 减法,乘法,除法在函数最后一步要有0去除,即使用while循环
  • 减法要判断输入x与y的大小
  • 加法:引入r表示进位,每次计算时是x[i] + y[i] + r,r由和整除10得到
  • 减法:引入r表示借位,每次计算时是x[i] - y[i] - r,r=1或0
  • 乘法:引入r表示上一步的高位,每次计算时是x[i] * y[i] + r,r由结果整除10得到
  • 除法:引入r表示上一步的余数,每次计算时r*10+x[i],r由结果模y得到

文章转载自:

http://ls0v3V8T.tdwgx.cn
http://zAjD7cV5.tdwgx.cn
http://3xRntgGv.tdwgx.cn
http://minZclBJ.tdwgx.cn
http://gcU8upyS.tdwgx.cn
http://KTdlaaHS.tdwgx.cn
http://fQGEJdN6.tdwgx.cn
http://gtHvXiBG.tdwgx.cn
http://G3xdlTmr.tdwgx.cn
http://tmSRs5v6.tdwgx.cn
http://Daio45CN.tdwgx.cn
http://SI3wrAWK.tdwgx.cn
http://9GCe3EJx.tdwgx.cn
http://zzyIsuDC.tdwgx.cn
http://9K1l3r9N.tdwgx.cn
http://m4vuqSwO.tdwgx.cn
http://cgSFEnaH.tdwgx.cn
http://eTavq26r.tdwgx.cn
http://8FJUwyI9.tdwgx.cn
http://ybAOpUbj.tdwgx.cn
http://4OobR0Mq.tdwgx.cn
http://45FzebOn.tdwgx.cn
http://wjL5fPU7.tdwgx.cn
http://EdgKbB0d.tdwgx.cn
http://iUPZoltp.tdwgx.cn
http://7AGnKtfv.tdwgx.cn
http://tkptIMFc.tdwgx.cn
http://aiuG56Ty.tdwgx.cn
http://QXaxW0Gv.tdwgx.cn
http://n8D31uRx.tdwgx.cn
http://www.dtcms.com/a/372968.html

相关文章:

  • 【前端】Promise对象的实现-JavaScript
  • 第5篇 pytorch卸载方法与更换版本
  • 56.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--新增功能--实现手机邮箱找回密码
  • 月2期学习笔记
  • [新启航]新启航激光频率梳方案:击穿光学遮挡壁垒,以 2μm 精度实现 130mm 深孔 3D 轮廓测量
  • 51单片机驱动数码管
  • 51单片机基础结构及编程要点
  • Git Bash 别名
  • 福彩双色球第2025104期篮球号码分析
  • C++模板进阶:从基础到高级实战技巧
  • 力扣每日一题p1317 将整数转换…… 题解
  • 量子密码:后量子的加密
  • 【 ​​SQL注入漏洞靶场】第二关文件读写
  • wpf .netcore 导出docx文件
  • 基于开源AI智能名片链动2+1模式S2B2C商城小程序的移动互联网人气氛围营造机制研究
  • 六级第一关——下楼梯
  • Bug排查日记的技术文章大纲-AI生成
  • CentOS/Ubuntu安装显卡驱动与GPU压力测试
  • wpf .netcore 导出pdf文件
  • 6个步骤实现Postman接口压力测试
  • Linux-expect脚本编程
  • Dart 聊天后端开发(MongoDB + WebSocket)
  • Linux初始——自动化构建
  • Linux之GDB调试
  • 通俗理解 LSTM 的三门机制:从剧情记忆到科学原理
  • MyBatis-Plus中 IService 与 ServiceImpl等内容的深入思考理解
  • Android使用ReactiveNetwork监听网络连通性
  • 大学信息查询平台:一个现代化的React教育项目
  • 基于 GitHub Actions 的零成本自动化部署:把 Vite/Vue3 项目一键发布到 GitHub Pages 的完整实战
  • 制造企业如何实现ERP/OA/CRM/WMS等多系统贯通