Problem4:用正方體填滿(12%)
在進入社會找工作時,通常會經過面試的過程,來決定是否要錄用這個人。假設今天你
去一家程式設計公司面試,面試的主考官出了一道題目。請你設計一個程式來解決下面的問
題:
給你一個長方體,請問最少要用幾個大小相同的正方體,才能把這個長方體填滿,你可
以使用的正方體大小不限,長方體及正方體的邊長必須均為正整數。
輸入說明:
第一行的數字,代表有幾個長方體。第二行開始的每一行,記錄了每個長方體長寬高的
邊長,邊長的範圍為[1, 1000]間的整數。
輸出說明:
對輸入的每個長方體,分別以一行輸出所使用正方體的個數。
輸入範例:
2
4 6 8
3 5 7
輸出範例:
24
105
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, t
For i = 1 To t
Input #1, a, b, c
Call A1(a * b * c, A2(a, b, c), a, b, c)
Next
Close
Close
End
End Sub
Sub A1(m, n, a, b, c)
Max = 1
For i = 1 To n
If m Mod i ^ 3 = 0 And a Mod i = 0 And b Mod i = 0 And c Mod i = 0 Then Max = i
Next
Print #2, m / Max ^ 3
End Sub
Function A2(a, b, c)
Dim s
k = a & " " & b & " " & c
s = Split(k)
For i = 0 To 2
For j = 0 To 1
If s(j) > s(j + 1) Then
tmp = s(i)
s(i) = s(i + 1)
s(i + 1) = tmp
End If
Next
Next
A2 = s(2)
End Function
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Dim X(3)
Input #1, N
For i = 1 To N
Input #1, X(1), X(2), X(3)
If X(1) < X(2) And X(1) < X(3) Then Y = M(X(2), X(3), X(1))
If X(2) < X(1) And X(2) < X(3) Then Y = M(X(1), X(3), X(2))
If X(3) < X(1) And X(3) < X(2) Then Y = M(X(1), X(2), X(3))
Print #2, (X(1) * X(2) * X(3)) / (Y ^ 3)
Next i
Close #2
Close #1
End
End Sub
Function M(A, B, Min)
ans = 1
For i = 2 To Min
If A Mod i = 0 And B Mod i = 0 And i > ans Then ans = i
Next i
M = ans
End Function