English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

Ugly Number的質因數問題 (10%)
Ugly Number的定義為:該數之質因數必須為 2, 3 或 5的組合(Ugly Number的質因數可分解為 ,其中x、y、z為正整數)。當x、y、z皆等於0時,為第一個Ugly Number,其值等於1。
在此列舉一串數列:1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15,這些就是前十一個Ugly Numbers。請寫一個程式求出第n個Ugly Number。
輸入規範
輸入檔案中包含好幾筆資料,每筆資料一行,包含一個整數n (0 n 10000)。
輸出規範
對每行輸入,輸出第n個Ugly Number。
輸入範例(test6.txt)
10
15
輸出範例(result6.txt)
12
24

2006-11-14 16:18:04 · 5 個解答 · 發問者 小古 1 in 電腦與網際網路 程式設計

5 個解答

'VB6.0
'專案→設定引用元件→Microsoft Scripting Runtime

Private Sub Form_Load()
 Dim fs1 As New FileSystemObject, ftxt1 As TextStream
 Dim fs2 As New FileSystemObject, ftxt2 As TextStream
 If Right(App.Path, 1) <> "\" Then
  Set ftxt1 = fs1.OpenTextFile(App.Path & "\test6.txt")
  Set ftxt2 = fs2.OpenTextFile(App.Path & "\result.txt", ForWriting, True)
 Else
  Set ftxt1 = fs1.OpenTextFile(App.Path & "test6.txt")
  Set ftxt2 = fs2.OpenTextFile(App.Path & "result.txt", ForWriting, True)
 End If
 Dim Var1 As Integer, i As Integer, j As Integer
 Do
  Var1 = Val(ftxt1.ReadLine)
  i = 0
  j = 0
  Do
   i = i + 1
   If Check(i) = True Then j = j + 1
  Loop Until j = Var1
  ftxt2.WriteLine i
 Loop Until ftxt1.AtEndOfLine
 ftxt1.Close
 ftxt2.Close
End Sub

Function Div(ByVal Value As Integer, DivNumber As Integer) As Integer
 If Value Mod DivNumber = 0 Then
  Div = Div(Value / DivNumber, DivNumber)
 Else
  Div = Value
 End If
End Function

Function Check(ByVal Value As Integer) As Boolean
 Value = Div(Value, 2)
 Value = Div(Value, 3)
 Value = Div(Value, 5)
 If Value = 1 Then
  Check = True
 Else
  Check = False
 End If
End Function

2006-11-15 04:18:45 · answer #1 · answered by 五百六 3 · 0 0

我不知道該怎麼說~~
妳要問也不並整個全部都複製下來
連輸入的檔案(test6.txt)跟輸出的檔案都打出來~~
唉~加油~~ 希望你比賽能拿高一點的成績!!

2006-12-08 13:42:04 · answer #2 · answered by 小均 2 · 0 0

抱歉…前兩天掛急診...來不急選到

2006-11-20 00:48:51 · answer #3 · answered by 小古 1 · 0 0

'程式又臭又長XD,不過經測試第2000個醜數跑出來的時間不到1秒'至於讀寫檔的部份上面兩位大大已寫得很清楚不再贅述Dim N%, K#()Private Sub Command1_Click()    N = Int(Val(InputBox("請輸入大於零之整數", "", 2000)))    If N < 1 Then Exit Sub    Cls    Ugly    Print K(N)End SubSub Ugly()    Dim I%, J%, L%, V%, T#, M#, B As Boolean, Y%(), S()        V = 1: ReDim K(V): K(V) = V    Do        ReDim S(L), Y(L)        For I = 0 To L            S(I) = Array(2, 3, 5)        Next        B = True        Do          T = 1          For I = 0 To L              T = T * S(I)(Y(I))          Next          If V > N Then             If T < M Then                V = V + 1: ReDim Preserve K(V): K(V) = T: B = False             Else                Exit Do             End If          Else             V = V + 1: ReDim Preserve K(V): K(V) = T: B = False             If T > M Then M = T          End If          For J = L To 0 Step -1              If Y(J) < 2 Then: Y(J) = Y(J) + 1: Exit For          Next          If J < 0 Then Exit Do          For I = J + 1 To L              Y(I) = Y(I - 1)          Next        Loop        L = L + 1    Loop Until B    For I = 1 To V - 1        For J = I To V            If K(I) > K(J) Then T = K(I): K(I) = K(J): K(J) = T        Next    NextEnd Sub

2006-11-18 22:27:14 補充:
嗯...何大很抱歉我不是要跟你吐槽,只是您的程式好像只能跑到第175個XD
邀請您來參考這問題:
http://tw.knowledge.yahoo.com/question/?qid=1106111707064&r=w#openions

2006-11-16 12:33:19 · answer #4 · answered by W.J.S. 7 · 0 0

Dim 醜數&(10000)
Private Sub Form_Load()
 Dim 測數&, 被除數&, 除數&, 記數&
 For 測數 = 1 To 10000
  被除數 = 測數: 除數 = 2
  Do Until 被除數 = 1
   Select Case 被除數 Mod 除數
    Case 0: 被除數 = 被除數 \ 除數
    Case Else
     Select Case 除數
      Case 2: 除數 = 3
      Case 3: 除數 = 5
      Case 5: Exit Do
     End Select
   End Select
  Loop
  If 被除數 = 1 Then 記數 = 記數 + 1: 醜數(記數) = 測數
 Next
End Sub
Private Sub Command1_Click()
 Dim 字$
 Open "test6.txt" For Input As #1
 Open "result6.txt" For Output As #2
 Do Until EOF(1)
  Input #1, 字
  Print #2, 醜數(Val(字))
 Loop
 Close
End Sub

2006-11-18 00:04:46 補充:
我的程式只要0.02秒

2006-11-19 11:02:25 補充:
W.J.S.
你說的對,當初我把第10000個當成那個醜數在10000之內
網路殺手....小古
我的程式不對不要選我的
W.J.S發問的醜數,我回答的程式才能用

2006-11-15 05:11:48 · answer #5 · answered by ? 2 · 0 0

fedest.com, questions and answers