'多位数相乘 Private Function Multi(ByVal Str1 As String, ByVal Str2 As String) As String Dim i As Integer, j As Integer Dim Carry As String, Result As String
If Len(Str1) < Len(Str2) Then '使Str2为较短的一个 Str1 = Str1 + Str2 Str2 = Left(Str1, Len(Str1) - Len(Str2)) Str1 = Right(Str1, Len(Str1) - Len(Str2)) End If
Carry = "0" For i = Len(Str1) To 1 Step -1 Result = AddStr(Multi1(Mid(Str1, i, 1), Str2), Carry) Multi = Right(Result, 1) & Multi Carry = Left(Result, Len(Result) - 1) Next i Multi = Carry & Multi
Do While Left(Multi, 1) = "0" And Len(Multi) > 1 Multi = Right(Multi, Len(Multi) - 1) Loop End Function'一位数乘多位数 Private Function Multi1(ByVal Str1 As Integer, ByVal Str2 As String) As String Dim i As Integer, j As Integer Dim Carry As Integer, Result As Integer
Carry = 0 For i = Len(Str2) To 1 Step -1 Result = Mid(Str2, i, 1) Result = Result * Str1 Multi1 = ((Result + Carry) Mod 10) & Multi1 Carry = (Result + Carry) \ 10 Next i Multi1 = Carry & Multi1
Do While Left(Multi1, 1) = "0" And Len(Multi1) > 1 Multi1 = Right(Multi1, Len(Multi1) - 1) Loop End FunctionPrivate Function AddStr(ByVal Str1 As String, ByVal Str2 As String) As String Dim i As Integer, j As Integer Dim Carry As Integer, Result As Integer Dim str3 As String
If Len(Str1) < Len(Str2) Then '使Str2为较短的一个 Str1 = Str1 + Str2 Str2 = Left(Str1, Len(Str1) - Len(Str2)) Str1 = Right(Str1, Len(Str1) - Len(Str2)) End If
Carry = 0 For i = Len(Str2) To 1 Step -1 Result = Val(Mid(Str1, Len(Str1) - Len(Str2) + i, 1)) + Val(Mid(Str2, i, 1)) + Carry AddStr = (Result Mod 10) & AddStr Carry = Result \ 10 Next i
For i = Len(Str1) - Len(Str2) To 1 Step -1 Result = Val(Mid(Str1, i, 1)) + Carry AddStr = (Result Mod 10) & AddStr Carry = Result \ 10 Next i
AddStr = Carry & AddStr
Do While Left(AddStr, 1) = "0" And Len(AddStr) > 1 AddStr = Right(AddStr, Len(AddStr) - 1) Loop End Function
'多位数相乘 Private Function Multi(ByVal Str1 As String, ByVal Str2 As String) As String Dim i As Integer, j As Integer Dim Carry As String, Result As String
If Len(Str1) < Len(Str2) Then '使Str2为较短的一个 Str1 = Str1 + Str2 Str2 = Left(Str1, Len(Str1) - Len(Str2)) Str1 = Right(Str1, Len(Str1) - Len(Str2)) End If
Carry = "0" For i = Len(Str1) To 1 Step -1 Result = AddStr(Multi1(Mid(Str1, i, 1), Str2), Carry) Multi = Right(Result, 1) & Multi Carry = Left(Result, Len(Result) - 1) Next i Multi = Carry & Multi
Do While Left(Multi, 1) = "0" And Len(Multi) > 1 Multi = Right(Multi, Len(Multi) - 1) Loop End Function'一位数乘多位数 Private Function Multi1(ByVal Str1 As Integer, ByVal Str2 As String) As String Dim i As Integer, j As Integer Dim Carry As Integer, Result As Integer
Carry = 0 For i = Len(Str2) To 1 Step -1 Result = Mid(Str2, i, 1) Result = Result * Str1 Multi1 = ((Result + Carry) Mod 10) & Multi1 Carry = (Result + Carry) \ 10 Next i Multi1 = Carry & Multi1
Do While Left(Multi1, 1) = "0" And Len(Multi1) > 1 Multi1 = Right(Multi1, Len(Multi1) - 1) Loop End FunctionPrivate Function AddStr(ByVal Str1 As String, ByVal Str2 As String) As String Dim i As Integer, j As Integer Dim Carry As Integer, Result As Integer Dim str3 As String
If Len(Str1) < Len(Str2) Then '使Str2为较短的一个 Str1 = Str1 + Str2 Str2 = Left(Str1, Len(Str1) - Len(Str2)) Str1 = Right(Str1, Len(Str1) - Len(Str2)) End If
Carry = 0 For i = Len(Str2) To 1 Step -1 Result = Val(Mid(Str1, Len(Str1) - Len(Str2) + i, 1)) + Val(Mid(Str2, i, 1)) + Carry AddStr = (Result Mod 10) & AddStr Carry = Result \ 10 Next i
For i = Len(Str1) - Len(Str2) To 1 Step -1 Result = Val(Mid(Str1, i, 1)) + Carry AddStr = (Result Mod 10) & AddStr Carry = Result \ 10 Next i
AddStr = Carry & AddStr
Do While Left(AddStr, 1) = "0" And Len(AddStr) > 1 AddStr = Right(AddStr, Len(AddStr) - 1) Loop End Function
晕,再发不出就不发了 '多位数相乘 Private Function Multi(ByVal Str1 As String, ByVal Str2 As String) As String Dim i As Integer, j As Integer Dim Carry As String, Result As String
If Len(Str1) < Len(Str2) Then '使Str2为较短的一个 Str1 = Str1 + Str2 Str2 = Left(Str1, Len(Str1) - Len(Str2)) Str1 = Right(Str1, Len(Str1) - Len(Str2)) End If
Carry = "0" For i = Len(Str1) To 1 Step -1 Result = AddStr(Multi1(Mid(Str1, i, 1), Str2), Carry) Multi = Right(Result, 1) & Multi Carry = Left(Result, Len(Result) - 1) Next i Multi = Carry & Multi
Do While Left(Multi, 1) = "0" And Len(Multi) > 1 Multi = Right(Multi, Len(Multi) - 1) Loop End Function'一位数乘多位数 Private Function Multi1(ByVal Str1 As Integer, ByVal Str2 As String) As String Dim i As Integer, j As Integer Dim Carry As Integer, Result As Integer
Carry = 0 For i = Len(Str2) To 1 Step -1 Result = Mid(Str2, i, 1) Result = Result * Str1 Multi1 = ((Result + Carry) Mod 10) & Multi1 Carry = (Result + Carry) \ 10 Next i Multi1 = Carry & Multi1
Do While Left(Multi1, 1) = "0" And Len(Multi1) > 1 Multi1 = Right(Multi1, Len(Multi1) - 1) Loop End FunctionPrivate Function AddStr(ByVal Str1 As String, ByVal Str2 As String) As String Dim i As Integer, j As Integer Dim Carry As Integer, Result As Integer Dim str3 As String
If Len(Str1) < Len(Str2) Then '使Str2为较短的一个 Str1 = Str1 + Str2 Str2 = Left(Str1, Len(Str1) - Len(Str2)) Str1 = Right(Str1, Len(Str1) - Len(Str2)) End If
Carry = 0 For i = Len(Str2) To 1 Step -1 Result = Val(Mid(Str1, Len(Str1) - Len(Str2) + i, 1)) + Val(Mid(Str2, i, 1)) + Carry AddStr = (Result Mod 10) & AddStr Carry = Result \ 10 Next i
For i = Len(Str1) - Len(Str2) To 1 Step -1 Result = Val(Mid(Str1, i, 1)) + Carry AddStr = (Result Mod 10) & AddStr Carry = Result \ 10 Next i
AddStr = Carry & AddStr
Do While Left(AddStr, 1) = "0" And Len(AddStr) > 1 AddStr = Right(AddStr, Len(AddStr) - 1) Loop End Function
long型才4个字节
改为双精度型的,也是溢出,不信你试试。
flyingscv(zlj)
大数计算函数,怎么写,教教我,好吗?
这样好麻烦,用字符串编写,那得用到好多笔算的东西,稍不留神就会出错,有没有更好的方法呢?
#include<iostream>
#include<iomanip>
#include<fstream>
#include<windows.h>using namespace std ;void main()
{
int i,j ;
int max=0,n ;
unsigned int tmp ;
cout<<"Input N="<<flush ;
cin>>n ;
//n<=10000
DWORD dwStart=:: GetTickCount(),dwStop ;
int result[9000]=
{
0
}
;
//int上界2147483647
result[0]=1 ;
for(i=1;i<=n;i++)
{
tmp=0 ;
for(j=0;j<=max;j++)
{
tmp+=result[j]*i ;
result[j]=tmp%10000 ;
tmp/=10000 ;
}
if(result[j]=tmp)
{
max++;
}
}
dwStop=:: GetTickCount();
// ofstream outf("out.txt");
for(j=max;j>=0;j--)
{
cout<<setw(4)<<setfill('0')<<result[j];
}
//
// outf.close();
// outf<<endl;
cout<<"\nLength="<<max*4<<"\nTime="<<(dwStop-dwStart)<<"ms"<<endl ;
}
这位小姐的思路还算不错,我会好好考虑考虑,若大家还有更好的思路或建议,请给我做出答复,谢谢大家。
这位先生,C++我没有学这么深,实在对不起,我看不懂,能不能不用C++呢,用VB好不好?
Private Function Multi(ByVal Str1 As String, ByVal Str2 As String) As String
Dim i As Integer, j As Integer
Dim Carry As String, Result As String
If Len(Str1) < Len(Str2) Then '使Str2为较短的一个
Str1 = Str1 + Str2
Str2 = Left(Str1, Len(Str1) - Len(Str2))
Str1 = Right(Str1, Len(Str1) - Len(Str2))
End If
Carry = "0"
For i = Len(Str1) To 1 Step -1
Result = AddStr(Multi1(Mid(Str1, i, 1), Str2), Carry)
Multi = Right(Result, 1) & Multi
Carry = Left(Result, Len(Result) - 1)
Next i
Multi = Carry & Multi
Do While Left(Multi, 1) = "0" And Len(Multi) > 1
Multi = Right(Multi, Len(Multi) - 1)
Loop
End Function'一位数乘多位数
Private Function Multi1(ByVal Str1 As Integer, ByVal Str2 As String) As String
Dim i As Integer, j As Integer
Dim Carry As Integer, Result As Integer
Carry = 0
For i = Len(Str2) To 1 Step -1
Result = Mid(Str2, i, 1)
Result = Result * Str1
Multi1 = ((Result + Carry) Mod 10) & Multi1
Carry = (Result + Carry) \ 10
Next i
Multi1 = Carry & Multi1
Do While Left(Multi1, 1) = "0" And Len(Multi1) > 1
Multi1 = Right(Multi1, Len(Multi1) - 1)
Loop
End FunctionPrivate Function AddStr(ByVal Str1 As String, ByVal Str2 As String) As String
Dim i As Integer, j As Integer
Dim Carry As Integer, Result As Integer
Dim str3 As String
If Len(Str1) < Len(Str2) Then '使Str2为较短的一个
Str1 = Str1 + Str2
Str2 = Left(Str1, Len(Str1) - Len(Str2))
Str1 = Right(Str1, Len(Str1) - Len(Str2))
End If
Carry = 0
For i = Len(Str2) To 1 Step -1
Result = Val(Mid(Str1, Len(Str1) - Len(Str2) + i, 1)) + Val(Mid(Str2, i, 1)) + Carry
AddStr = (Result Mod 10) & AddStr
Carry = Result \ 10
Next i
For i = Len(Str1) - Len(Str2) To 1 Step -1
Result = Val(Mid(Str1, i, 1)) + Carry
AddStr = (Result Mod 10) & AddStr
Carry = Result \ 10
Next i
AddStr = Carry & AddStr
Do While Left(AddStr, 1) = "0" And Len(AddStr) > 1
AddStr = Right(AddStr, Len(AddStr) - 1)
Loop
End Function
你怎么知道viena(晓琴)是小姐,又怎么知道sonique(计算机与管理我选谁) 是先生?!
Private Function Multi(ByVal Str1 As String, ByVal Str2 As String) As String
Dim i As Integer, j As Integer
Dim Carry As String, Result As String
If Len(Str1) < Len(Str2) Then '使Str2为较短的一个
Str1 = Str1 + Str2
Str2 = Left(Str1, Len(Str1) - Len(Str2))
Str1 = Right(Str1, Len(Str1) - Len(Str2))
End If
Carry = "0"
For i = Len(Str1) To 1 Step -1
Result = AddStr(Multi1(Mid(Str1, i, 1), Str2), Carry)
Multi = Right(Result, 1) & Multi
Carry = Left(Result, Len(Result) - 1)
Next i
Multi = Carry & Multi
Do While Left(Multi, 1) = "0" And Len(Multi) > 1
Multi = Right(Multi, Len(Multi) - 1)
Loop
End Function'一位数乘多位数
Private Function Multi1(ByVal Str1 As Integer, ByVal Str2 As String) As String
Dim i As Integer, j As Integer
Dim Carry As Integer, Result As Integer
Carry = 0
For i = Len(Str2) To 1 Step -1
Result = Mid(Str2, i, 1)
Result = Result * Str1
Multi1 = ((Result + Carry) Mod 10) & Multi1
Carry = (Result + Carry) \ 10
Next i
Multi1 = Carry & Multi1
Do While Left(Multi1, 1) = "0" And Len(Multi1) > 1
Multi1 = Right(Multi1, Len(Multi1) - 1)
Loop
End FunctionPrivate Function AddStr(ByVal Str1 As String, ByVal Str2 As String) As String
Dim i As Integer, j As Integer
Dim Carry As Integer, Result As Integer
Dim str3 As String
If Len(Str1) < Len(Str2) Then '使Str2为较短的一个
Str1 = Str1 + Str2
Str2 = Left(Str1, Len(Str1) - Len(Str2))
Str1 = Right(Str1, Len(Str1) - Len(Str2))
End If
Carry = 0
For i = Len(Str2) To 1 Step -1
Result = Val(Mid(Str1, Len(Str1) - Len(Str2) + i, 1)) + Val(Mid(Str2, i, 1)) + Carry
AddStr = (Result Mod 10) & AddStr
Carry = Result \ 10
Next i
For i = Len(Str1) - Len(Str2) To 1 Step -1
Result = Val(Mid(Str1, i, 1)) + Carry
AddStr = (Result Mod 10) & AddStr
Carry = Result \ 10
Next i
AddStr = Carry & AddStr
Do While Left(AddStr, 1) = "0" And Len(AddStr) > 1
AddStr = Right(AddStr, Len(AddStr) - 1)
Loop
End Function
'多位数相乘
Private Function Multi(ByVal Str1 As String, ByVal Str2 As String) As String
Dim i As Integer, j As Integer
Dim Carry As String, Result As String
If Len(Str1) < Len(Str2) Then '使Str2为较短的一个
Str1 = Str1 + Str2
Str2 = Left(Str1, Len(Str1) - Len(Str2))
Str1 = Right(Str1, Len(Str1) - Len(Str2))
End If
Carry = "0"
For i = Len(Str1) To 1 Step -1
Result = AddStr(Multi1(Mid(Str1, i, 1), Str2), Carry)
Multi = Right(Result, 1) & Multi
Carry = Left(Result, Len(Result) - 1)
Next i
Multi = Carry & Multi
Do While Left(Multi, 1) = "0" And Len(Multi) > 1
Multi = Right(Multi, Len(Multi) - 1)
Loop
End Function'一位数乘多位数
Private Function Multi1(ByVal Str1 As Integer, ByVal Str2 As String) As String
Dim i As Integer, j As Integer
Dim Carry As Integer, Result As Integer
Carry = 0
For i = Len(Str2) To 1 Step -1
Result = Mid(Str2, i, 1)
Result = Result * Str1
Multi1 = ((Result + Carry) Mod 10) & Multi1
Carry = (Result + Carry) \ 10
Next i
Multi1 = Carry & Multi1
Do While Left(Multi1, 1) = "0" And Len(Multi1) > 1
Multi1 = Right(Multi1, Len(Multi1) - 1)
Loop
End FunctionPrivate Function AddStr(ByVal Str1 As String, ByVal Str2 As String) As String
Dim i As Integer, j As Integer
Dim Carry As Integer, Result As Integer
Dim str3 As String
If Len(Str1) < Len(Str2) Then '使Str2为较短的一个
Str1 = Str1 + Str2
Str2 = Left(Str1, Len(Str1) - Len(Str2))
Str1 = Right(Str1, Len(Str1) - Len(Str2))
End If
Carry = 0
For i = Len(Str2) To 1 Step -1
Result = Val(Mid(Str1, Len(Str1) - Len(Str2) + i, 1)) + Val(Mid(Str2, i, 1)) + Carry
AddStr = (Result Mod 10) & AddStr
Carry = Result \ 10
Next i
For i = Len(Str1) - Len(Str2) To 1 Step -1
Result = Val(Mid(Str1, i, 1)) + Carry
AddStr = (Result Mod 10) & AddStr
Carry = Result \ 10
Next i
AddStr = Carry & AddStr
Do While Left(AddStr, 1) = "0" And Len(AddStr) > 1
AddStr = Right(AddStr, Len(AddStr) - 1)
Loop
End Function