【开源】int,long long去一边去:高精度大合集!
加法 \(add\)
string add(string s1, string s2) { //时间复杂度 O(log n)
string res = "";
int c = 0, i = 0;
while (i<s1.size() || i < s2.size() || c>0) {
int a = (i<s1.size()) ? (s1[s1.size() - i - 1] - '0') : 0;
int b = (i<s2.size()) ? (s2[s2.size() - i - 1] - '0') : 0;
res += ((a + b + c) % 10) + '0';
c = (a + b + c) / 10;
i++;
}
reverse(res.begin(), res.end());
return res;
}
减法 \(sub\)
string sub(string a, string b) { //时间复杂度 O(log n)
string res;
int c = 0;
if (a.size() < b.size()) a = string(b.size() - a.size(), '0') + a;
else if (a.size() > b.size()) b = string(a.size() - b.size(), '0') + b;
for (int i = a.size() - 1; i >= 0; i--) {
int d = a[i] - b[i] - c;
if (d < 0) d += 10, c = 1;
else c = 0;
res.push_back(d + '0');
}
reverse(res.begin(), res.end());
while (res.size() > 1 && res[0] == '0') {
res.erase(res.begin());
}
return res;
}
乘法 \(mul\)
string mul(string num1, string num2) { //时间复杂度 O(n^2)
string res(int(num1.length()) + int(num2.length()), '0');
for (int i = int(num1.length()) - 1; i >= 0; i--) {
for (int j = int(num2.length()) - 1; j >= 0; j--) {
int pd = (num1[i] - '0') * (num2[j] - '0');
int p1 = i + j;
int p2 = i + j + 1;
int sum = pd + (res[p2] - '0');
res[p2] = sum % 10 + '0';
res[p1] += sum / 10;
}
}
int i = 0;
while (i<int(res.length()) && res[i] == '0') {
i++;
}
return i == int(res.length()) ? "0" : res.substr(i);
}
\]
#include <iostream>
#include <vector>
using namespace std;
// 将字符串转换为整数数组
vector<int> strToVec(string str) {
vector<int> vec;
for (int i = str.length() - 1; i >= 0; i--) {
vec.push_back(str[i] - '0');
}
return vec;
}
// 将整数数组转换为字符串
string vecToStr(vector<int> vec) {
string str = "";
for (int i = vec.size() - 1; i >= 0; i--) {
str += to_string(vec[i]);
}
return str;
}
// 高精度加法
vector<int> add(vector<int> num1, vector<int> num2) {
vector<int> result;
int carry = 0;
int len1 = num1.size();
int len2 = num2.size();
int maxLen = max(len1, len2);
for (int i = 0; i < maxLen; i++) {
int digit1 = i < len1 ? num1[i] : 0;
int digit2 = i < len2 ? num2[i] : 0;
int sum = digit1 + digit2 + carry;
result.push_back(sum % 10);
carry = sum / 10;
}
if (carry != 0) {
result.push_back(carry);
}
return result;
}
// 高精度乘法
vector<int> mul(vector<int> num1, vector<int> num2) {
int len1 = num1.size();
int len2 = num2.size();
// 递归终止条件
if (len1 == 0 || len2 == 0) {
return vector<int>();
}
// 递归基
if (len1 == 1 && len2 == 1) {
vector<int> result;
int product = num1[0] * num2[0];
result.push_back(product % 10);
if (product >= 10) {
result.push_back(product / 10);
}
return result;
}
// 将数字分为两部分
int mid = min(len1, len2) / 2;
vector<int> num1Low(num1.begin(), num1.begin() + mid);
vector<int> num1High(num1.begin() + mid, num1.end());
vector<int> num2Low(num2.begin(), num2.begin() + mid);
vector<int> num2High(num2.begin() + mid, num2.end());
// 递归计算
vector<int> z0 = mul(num1Low, num2Low);
vector<int> z1 = mul(num1High, num2High);
vector<int> z2 = mul(add(num1Low, num1High), add(num2Low, num2High));
z2 = add(z2, z0);
z2 = add(z2, z1);
// 合并结果
vector<int> result;
result.insert(result.end(), z0.begin(), z0.end());
result.insert(result.begin() + mid, z2.begin(), z2.end());
result.insert(result.begin() + 2 * mid, z1.begin(), z1.end());
return result;
}
int main() {
string str1, str2;
cout << "请输入两个整数:" << endl;
cin >> str1 >> str2;
vector<int> num1 = strToVec(str1);
vector<int> num2 = strToVec(str2);
vector<int> result = mul(num1, num2);
string strResult = vecToStr(result);
cout << "两个整数的乘积为:" << endl;
cout << strResult << endl;
return 0;
}
除法&取余 \(divi\)
两个正数相除,商为\(quotient\),余数为\(residue\)
int compare(string str1, string str2) {
if (str1.length() > str2.length()) return 1;
else if (str1.length() < str2.length()) return -1;
else return str1.compare(str2);
}
string sub(string a, string b) { //时间复杂度 O(log n)
string res;
int c = 0;
if (a.size() < b.size()) a = string(b.size() - a.size(), '0') + a;
else if (a.size() > b.size()) b = string(a.size() - b.size(), '0') + b;
for (int i = a.size() - 1; i >= 0; i--) {
int d = a[i] - b[i] - c;
if (d < 0) d += 10, c = 1;
else c = 0;
res.push_back(d + '0');
}
reverse(res.begin(), res.end());
while (res.size() > 1 && res[0] == '0') {
res.erase(res.begin());
}
return res;
}
string mul(string num1, string num2) { //时间复杂度 O(n^2)
string res(int(num1.length()) + int(num2.length()), '0');
for (int i = int(num1.length()) - 1; i >= 0; i--) {
for (int j = int(num2.length()) - 1; j >= 0; j--) {
int pd = (num1[i] - '0') * (num2[j] - '0');
int p1 = i + j;
int p2 = i + j + 1;
int sum = pd + (res[p2] - '0');
res[p2] = sum % 10 + '0';
res[p1] += sum / 10;
}
}
int i = 0;
while (i<int(res.length()) && res[i] == '0') {
i++;
}
return i == int(res.length()) ? "0" : res.substr(i);
}
void divi(string str1, string str2, string& quotient, string& residue) {
quotient = residue = "";
if (str2 == "0") {
quotient = residue = "ERROR";
return;
}
if (str1 == "0") {
quotient = residue = "0";
return;
}
int res = compare(str1, str2);
if (res < 0) {
quotient = "0";
residue = str1;
return;
}
else if (res == 0) {
quotient = "1";
residue = "0";
return;
}
else {
int len1 = str1.length();
int len2 = str2.length();
string tempstr;
tempstr.append(str1, 0, len2 - 1);
for (int i = len2 - 1; i < len1; i++) {
tempstr = tempstr + str1[i];
tempstr.erase(0, tempstr.find_first_not_of('0'));
if (tempstr.empty()) tempstr = "0";
for (char ch = '9'; ch >= '0'; ch--) {
string str, tmp;
str = str + ch;
tmp = mul(str2, str);
if (compare(tmp, tempstr) <= 0) {
quotient = quotient + ch;
tempstr = sub(tempstr, tmp);
break;
}
}
}
residue = tempstr;
}
quotient.erase(0, quotient.find_first_not_of('0'));
if (quotient.empty()) quotient = "0";
}
【开源】int,long long去一边去:高精度大合集!的更多相关文章
- 直接拿来用!Facebook移动开源项目大合集
直接拿来用!Facebook移动开源项目大合集 时间:2014-04-22 15:37 作者:唐小引 随着iOS依赖管理工具CocoaPods和大量第三方开源库成熟起来,业界积累了大量的优秀开源项目. ...
- TypeScript 优秀开源项目大合集
TypeScript出来有段时间了,也冒出了很多用TypeScript开发的优秀开源项目,搜寻了一些基于TypeScript项目,分享给大家: https://github.com/brookshi/ ...
- 400多个开源项目以及43个优秀的Swift开源项目-Swift编程语言资料大合集
Swift 基于C和Objective-C,是供iOS和OS X应用编程的全新语言,更加高效.现代.安全,可以提升应用性能,同时降低开发难度. Swift仍然处于beta测试的阶段,会在iOS 8发布 ...
- iOS开源项目教程大合集
UI篇 1.MMDrawerController http://www.cnblogs.com/shangdahao/p/3142204.html 2.SVPullToRefresh http://w ...
- Tomcat 7 的七大新特性(更容易将Tomcat内嵌到应用去中去 )
Tomcat的7引入了许多新功能,并对现有功能进行了增强.很多文章列出了Tomcat 7的新功能,但大多数并没有详细解释它们,或指出它们的不足,或提供代码示例.本文将明确描述TOMCAT 7中七个最显 ...
- 最新最全的 Android 开源项目合集
原文链接:https://github.com/opendigg/awesome-github-android-ui 在 Github 上做了一个很新的 Android 开发相关开源项目汇总,涉及到 ...
- 深度学习优质学习项目大放送!-AI Studio精选开源项目合集推荐
近期 在AI Studio上发现了不少优质的开源深度学习项目,从深度学习入门到进阶,涵盖了CV.NLP.生成对抗网络.强化学习多个研究方向,还有最新的动态图,都以NoteBook的方式直接开源出来,并 ...
- 【年度开源、工具合集】牛津计划,DMTK,Graph Engine…提高你的工作效率!
本篇合集包括以下三个部分的内容: 1.微软亚洲研究院过去一年的所有开源合集,如分布式机器学习工具包DMTK等. 2.利用微软研究院的技术提高工作效率的工具合集,如让没有机器学习背景的开发人员也能开发出 ...
- 超全的 Vue 开源项目合集,签收一下
超全的 Vue 开源项目合集,签收一下 xiaoge2016 前端开发 1周前 作者:xiaoge2016 链接: https://my.oschina.net/u/3018050/blog/2049 ...
- 开源ckplayer 网页播放器去logo去广告去水印修改
功能设置介绍 本教程涉及到以下各点,点击对应标题页面将直接滑动到相应内容: 1:修改或去掉播放器前置logo 2:修改或去掉右上角的logo 3:修改.关闭.设置滚动文字广告 4:去掉右边的开关灯分享 ...
随机推荐
- 王道oj/problem23
网址:oj.lgwenda.problem/23 代码: #define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include<stri ...
- quarkus依赖注入之四:选择注入bean的高级手段
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是<quarkus依赖注入> ...
- TRL 正式推出,来训练你的首个 RLHF 模型吧!
我们正式向大家介绍 TRL--Transformer Reinforcement Learning.这是一个超全面的全栈库,包含了一整套工具用于使用强化学习 (Reinforcement Learni ...
- CI+JUnit5并发单测机制创新实践
一. 现状·问题 针对现如今高并发场景的业务系统,"并发问题" 终归是必不可少的一类(占比接近10%),每次出现问题和事故后,需要耗费大量人力成本排查分析并修复.那如果能在事前尽可 ...
- PE文件结构2(实现PE文件载入)
现在我们已经学完了PE文件格式,但是尚还停留在纸上谈兵的阶段,作为Windows系统上的可执行文件格式,PE文件结构总是和结构体,指针等紧密联系在一起的.理解它的最好方法就是通过写一个类似LordPE ...
- React Router 6 快速上手
1.概述 React Router 以三个不同的包发布到 npm 上,它们分别为: react-router: 路由的核心库,提供了很多的:组件.钩子. react-router-dom: 包含rea ...
- Vue【原创】千位符输入框(不仅只是过滤器哦)
最近和一个做金融的朋友讨论到千位符输入的问题,后来一想貌似自己项目中也会经常碰到金额数字这种输入框,要么自己做一个吧. 首先肯定要有一个正则表达式,也就是过滤器的方案里面常用的正则: 1 filter ...
- torch.nn基础学习教程 | PyTorch nn Basic Tutorial
基于torch.nn搭建神经网络的基础教程大纲: 1. 引言 在我们开始深入探讨torch.nn之前,我们首先需要理解PyTorch及其神经网络库的基础知识.这一部分的内容将帮助你对PyTorch有一 ...
- Python API接口对接详解与实践
在数字化时代,数据交互已经成为各种应用的必备功能.API(应用程序编程接口)就是实现不同应用程序之间数据交互的一种方式.Python作为一种功能强大的编程语言,也提供了许多用于对接API的库和框架 ...
- OpenStack-T版+Ceph
OpenStack OpenStack 中有哪些组件 keystone:授权 [授权后各个组件才可以进行相应的功能] Keystone 认证所有 OpenStack 服务并对其进行授权.同时,它也是所 ...