- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章C++实现大整数乘法(字符串乘法)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例为大家分享了C++实现大整数乘法的具体代码,供大家参考,具体内容如下 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#include<iostream>
#include<algorithm>
#include<string>
using
namespace
std;
string add(string a,string b)
{
if
(a.length()==0)
return
b;
if
(b.length()==0)
return
a;
a.length()<b.length()?a.swap(b):a.length();
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
int
flag=0;
for
(
int
i=0;i<a.length();i++)
{
int
aInt=a[i]-
'0'
;
int
bInt=0;
if
(i<b.length())
bInt=b[i]-
'0'
;
int
result=aInt+bInt+flag;
a[i]=result%10+
'0'
;
flag=result/10;
}
if
(flag!=0)
{
a=a+(
char
)(flag+
'0'
);
}
reverse(a.begin(),a.end());
return
a;
}
string multiply(std::string strMultiplierA,
char
x)
{
int
b=x-
'0'
;
int
flag=0;
for
(
int
i=strMultiplierA.length()-1;i>=0;i--)
{
int
a=strMultiplierA[i]-
'0'
;
int
result=a*b+flag;
strMultiplierA[i]=result%10+
'0'
;
flag=result/10;
}
if
(flag!=0)
strMultiplierA=(
char
)(flag+
'0'
)+strMultiplierA;
while
(strMultiplierA.length()>1&&strMultiplierA[0]==
'0'
)
strMultiplierA=strMultiplierA.substr(1,strMultiplierA.length());
return
strMultiplierA;
}
/*****************************************************************************
Prototype : multiply
Description : 两个任意长度的长整数相乘, 输出结果
Input Param :
const std::string strMultiplierA 乘数A
const std::string strMultiplierB 乘数B
Output :
std::string strRst 乘法结果
Return Value :
int 0 正确
-1 异常
*****************************************************************************/
int
multiply (
const
std::string strMultiplierA,
const
std::string strMultiplierB, std::string &strRst)
{
/* 在这里实现功能 */
if
(strMultiplierA.length()<=0||strMultiplierB.length()<=0)
return
-1;
bool
flag=
false
;
//false"+" true"-"
string strA=strMultiplierA,strB=strMultiplierB;
if
(strMultiplierA[0]==
'-'
)
{
flag=~flag;
strA=strMultiplierA.substr(1,strMultiplierA.length());
}
if
(strMultiplierB[0]==
'-'
)
{
flag==
true
?flag=
false
:flag=
true
;
strB=strMultiplierB.substr(1,strMultiplierB.length());
}
for
(
int
i=strB.length()-1;i>=0;i--)
{
string result=multiply(strA,strB[i]);
int
j=i;
while
(++j<strB.length())
result=result+
"0"
;
// while(result.length()>1&&result[0]=='0')
// result=result.substr(1,result.length());
strRst=add(strRst,result);
}
while
(strRst.length()>1&&strRst[0]==
'0'
)
strRst=strRst.substr(1,strRst.length());
if
(flag==
true
&&strRst!=
"0"
)
strRst=
'-'
+strRst;
return
0;
}
int
main()
{
std::string strResult =
""
;
multiply(
"-5489324"
,
"0"
, strResult);
cout<<strResult<<endl;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://blog.csdn.net/qiuchenl/article/details/17752073 。
最后此篇关于C++实现大整数乘法(字符串乘法)的文章就讲到这里了,如果你想了解更多关于C++实现大整数乘法(字符串乘法)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
如果矩阵A在X中,矩阵B在Y中。 进行乘法运算只是 Z = X*Y。正确假设两个数组的大小相同。 如何使用 for 循环计算它? 最佳答案 ja72 的anwser 是错误的,请查看我在其下的评论以了
我有一个 C 程序,它有 n 次乘法(单次乘法和 n 次迭代),我发现另一个逻辑有 n/2 次迭代(1 次乘法 + 2 次加法)。我知道两者都是 O(n) 的复杂性。但就 CPU 周期而言。哪个更快?
我有一个矩阵x: x <- matrix(1:8, nrow = 2, ncol = 4, byrow = 2) # [,1] [,2] [,3] [,4] #[1,] 1 2 3
我有一个矩阵x: x <- matrix(1:8, nrow = 2, ncol = 4, byrow = 2) # [,1] [,2] [,3] [,4] #[1,] 1 2 3
我正在创建一个基于电影 InTime 的 Minecraft 插件,并尝试创建代码,在玩家死亡时玩家将失去 25% 的时间。 当前代码是: String minus = itapi.getTimeSt
我正在尝试将 2 个矩阵与重载的 * 运算符相乘并打印结果。虽然看起来我不能为重载函数提供超过 1 个参数。如何将这两个矩阵传递给重载函数?请在下面查看我的实现。 #include #include
为什么在 Java 中使用 .*?例如 double probability = 1.*count/numdata; 给出相同的输出: double probability = count/numda
如果我尝试将两个值与单位相乘,则会出现意外错误。 $test: 10px; .testing{ width: $test * $test; } result: 100px*px isn't a v
我正在尝试计算库存中所有产品的总值(value)。表中的每种产品都有价格和数量。因此,我需要将每种产品的价格乘以数量,然后将所有这些加在一起以获得所有产品的总计。根据上一个问题,我现在可以使用 MyS
我正在尝试计算库存中所有产品的总值(value)。表中的每种产品都有价格和数量。因此,我需要将每种产品的价格乘以数量,然后将所有这些加在一起以获得所有产品的总计。根据上一个问题,我现在可以使用 MyS
大家好,我有以下代码行 solution first = mylist.remove((int)(Math.random() * mylist)); 这给了我一个错误说明 The operator *
我必须做很多乘法运算。如果我考虑效率,那么我应该使用位运算而不是常规的 * 运算吗?如果有差异如何进行位运算?提前致谢.. 最佳答案 不,您应该使用乘法运算符,让优化编译器决定如何最快地完成它。 您会
两个 n 位数字 A 和 B 的乘法可以理解为移位的总和: (A << i1) + (A << i2) + ... 其中 i1, i2, ... 是 B 中设置为 1 的位数。 现在让我们用 OR
我想使用 cuda 6 进行 bool 乘法,但我无法以正确的方式做到这一点。B 是一个 bool 对称矩阵,我必须进行 B^n bool 乘法。 我的 C++ 代码是: for (m=0; m
我正在编写一个定点类,但遇到了一些问题...乘法、除法部分,我不确定如何模拟。我对部门运算符(operator)进行了非常粗暴的尝试,但我确信这是错误的。到目前为止,它是这样的: class Fixe
我有TABLE_A我需要创建 TABLE_A_FINAL 规则: 在TABLE_A_FINAL中我们有包含 ID_C 的所有可能组合的行如果在 TABLE_A与 ID_C 的组合相同我们乘以 WEIG
这个问题在这里已经有了答案: Simple way to repeat a string (32 个答案) 关闭 6 年前。 我有一个任务是重复字符乘以它例如用户应该写重复输入 3 R 输出的字母和
我最近学习了C++的基础知识。我发现了一些我不明白的东西。这是让我有点困惑的程序。 #include using namespace std; int main()
我有两个列表: list_a = list_b = list(范围(2, 6)) final_list = [] 我想知道如何将两个列表中的所有值相乘。我希望我的 final_list 包含 [2*2
如何修改此代码以适用于任何基数? (二进制、十六进制、基数 10 等) int mult(int a, int b, int base){ if((a<=base)||(b<=base)){
我是一名优秀的程序员,十分优秀!