一道面試題, 求 1000!的位數 VBS實現

2021-04-02 12:23:55 字數 4797 閱讀 1275

'大數加法

'引數 x: 加數, y: 被加數

'以陣列儲存

function

getaddresult(x, y)

dim arrr(), strx, stry, lx, ly

strx = strreverse(x)

stry = strreverse(y)

lx = len(strx)

ly = len(stry)

'msgbox lx

dim i, imax, ijinwei

if lx > ly then

imax = lx

else

imax = ly

end if

redim arrr(imax)

ijinwei = 0

for i = 1 to imax

arrr(i) = int("0" & mid(strx, i, 1)) + int("0" & mid(stry, i, 1)) + ijinwei

ijinwei = arrr(i) / 10

arrr(i) = right(arrr(i), 1)

next

dim strresult

strresult = strreverse(join(arrr, ""))

if left(strresult, 1)  = "0" or ijinwei = 1 then

strresult = "1" & strresult

end if

getaddresult = strresult

end function

'加法'引數 x: 加數, y: 被加數

'以字串構造

'該方法經過試驗不可行..速度太慢!

function

getaddnresult(x, y)

dim strx, stry, lx, ly

strx = strreverse(x)

stry = strreverse(y)

lx = len(strx)

ly = len(stry)

dim i, imax, ijinwei

if lx > ly then

imax = lx

else

imax = ly

end if

dim itemp, strresult

ijinwei = 0

itemp = 0

for i = 1 to imax

itemp = int("0" & mid(strx, i, 1)) + int("0" & mid(stry, i, 1)) + ijinwei

ijinwei = itemp / 10

strresult = (itemp mod 10) & strresult

next

if left(strresult, 1)  = "0" or ijinwei = 1 then

strresult = "1" & strresult

end if

getaddnresult = strresult

end function

'乘法'引數 x: 大數, y: 小數(一位)

function

getmulresult(x, y)

getmulresult = 0

dim i

for i = 1 to y

getmulresult = getaddresult(getmulresult, x)

next

end function

'乘法'引數 x: 大數, y: 小數

function

getmultresult(x, y)

getmultresult = 0

dim str, lx, ly, i

ly = len(y)

for i = 1 to ly

getmultresult = getaddresult(getmultresult, getmulresult(x, cint(mid(y, i, 1))) & string(ly - i, "0"))

next

end function

'乘法'引數 x: 大數, y: 大數

function

getmulcresult(x, y)

if len(x) > len(y) then

getmulcresult = getmultresult(x, y)

else

getmulcresult = getmultresult(y, x)

end if

end function

'階乘'引數 x: 自然數

function

getfactorialresult(x)

getfactorialresult = x

dim i

for i = (x - 1) to 2 step - 1

getfactorialresult = getmulcresult(getfactorialresult, i)

next

end function

'直接求階乘位數

'引數 x: 自然數

'原作: aiur2000(閉關失敗,走火入魔,開關拉!)

'vbs**實現: xiaoyuehen

function

getfactoriallength(x)

dim ilen, isum, ival, i

'思路:因為不計算具體值,所以沒必要全部算出,只用計算能影響到結果的位數即可,同時要加入下次影響結果的值.

'做法:每次的積取下次乘數的位數的2倍即可.

'下乙個乘數的位數

ilen = 0

'位數isum = 0

'積ival = 1

for i = 1 to x

ilen = len(i)

'如果積超過乘數字數2倍,後面的記入彙總位數

if len(ival) > ilen * 2 then

isum = isum + (len(ival) - ilen * 2)

ival = left(ival, ilen * 2)

end if

ival = ival * i

next

getfactoriallength = len(ival) + isum

end function

'下面開始各項測試

dim t, x, y

t = timer

x = string(1000, "9")

y = string(1000, "9")

'y = string(200, "9")

'msgbox " string 總用時: " & formatnumber(timer - t) & " 秒"

't = timer

'call getaddresult(x, y)

'msgbox " getaddresult: " & "getaddresult(x, y)" & " 總用時: " & formatnumber(timer - t) & " 秒"

t = timer

'call getfactoriallength(1000)

msgbox " getfactoriallength(1000): " & getfactoriallength(1000) & " 總用時: " & formatnumber(timer - t) & " 秒"

't = timer

'call getmulresult(x, y)

'msgbox " getmulresult: " & getmulresult(x, y) & " 總用時: " & formatnumber(timer - t) & " 秒"

't = timer

'call getmulresult(x, y)

'msgbox " getmultresult: " & getmultresult(x, y) & " 總用時: " & formatnumber(timer - t) & " 秒"

't = timer

'call getmulresult(x, y)

'msgbox " getmulcresult: " & getmulcresult(x, y) & " 總用時: " & formatnumber(timer - t) & " 秒"

't = timer

'call getaddresult(x, y)

'msgbox " getaddresult 總用時: " & formatnumber(timer - t) & " 秒"

't = timer

'call getaddnresult(x, y)

'msgbox " getaddnresult 總用時: " & formatnumber(timer - t) & " 秒"

'x = 300

't = timer

'call getfactorialresult(x)

'msgbox " getfactorialresult(" & x & ") = " & getfactorialresult(x) & " 總用時: " & formatnumber(timer - t) & " 秒"

'msgbox " 共 " & len(getfactorialresult(x)) & " 位, 用時: " & formatnumber(timer - t) & " 秒"

一道面試題

一道面試題 射擊運動員10發打中90環有多少種可能,請編寫程式計算出來,並列印出結果,0環和10環均有效。打中90環就是沒打中10環,所以打中90環跟打中10環的可能性是一樣的。然後開始遞迴狂打槍,一到10就記錄 if params i 10 在迴圈的控制中已經排除了大於10的可能性 i 10 pa...

一道面試題

前些時候在找工作,就在準備結束此次找工作歷程的時候,去了一家公司面試,去了之後技術經理直接帶到一台電腦旁,給了一張紙條,上面是這樣的題目 用c或c 來實現 1 建立一棵樹,該樹的深度是隨機的,每個節點的位元組點數是隨機的。2 給每個節點分配一段隨機大小的記憶體空間,給每個節點賦乙個隨機數。3 遍歷這...

一道面試題

如果n為偶數,則將它除以2,如果n為奇數,則將它加1或者減1。問對於乙個給定的n,怎樣才能用最少的步驟將它變到1。例如 n 61 n 60 n 2 30 n 2 15 n 16 n 2 8 n 2 4 n 2 2 n 2 1 public class myclass public static vo...