以前好像也看到过你发的帖子,总觉得语言语文是美术老师教的,
就说一下按照理解想的步骤吧,先把所有的数列出来,然后找到一个出现的行数最多的一个,取出这个数字的同时把相关的那些行都排除,继续重复直到把所有行都排除掉- fn = "numbers.txt"
-
- Set data = ReadNumbers(fn)
-
- Do While data.Count > 0
- x = FetchOneNumber(data)
- WScript.Echo x
- Loop
-
-
- Function ReadNumbers(filespec)
- Dim objData, objFso, objFile, arrLine
- Set objData = WScript.CreateObject("Scripting.Dictionary")
- Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
- Set objFile = objFso.OpenTextFile(filespec, 1)
- Do While Not objFile.AtEndOfStream
- objData.Add objFile.Line, Split(objFile.ReadLine())
- Loop
- Set ReadNumbers = objData
- End Function
-
- Function FetchOneNumber(objDictionary)
- Dim objNum, objLine, lines, l, nums, n, i, j, theOne, theFreq
- Set objNum = WScript.CreateObject("Scripting.Dictionary")
- ' 将数据转换成 key:NUMBER/value:LINE 格式
- lines = objDictionary.Keys
- For i = 0 To UBound(lines)
- l = lines(i)
- nums = objDictionary.Item(l)
- For j = 0 To UBound(nums)
- n = nums(j)
- If Not objNum.Exists(n) Then
- Set objLine = WScript.CreateObject("Scripting.Dictionary")
- objNum.Add n, objLine
- End If
- objNum.Item(n).Add l, 1
- Next
- Next
- ' 找到出现频率最高的数字
- theOne = -1
- theFreq = 0
- nums = objNum.Keys
- For i = 0 To UBound(nums)
- n = nums(i)
- Set objLine = objNum.Item(n)
- If objLine.Count > theFreq Then
- theOne = n
- theFreq = objLine.Count
- End If
- Next
- ' 剔除已使用的行
- Set objLine = objNum.Item(theOne)
- lines = objLine.Keys
- Dim strMsg
- strMsg = "数字 " & theOne & " 出现 " & objLine.Count & " 次,位于第 "
- For j = 0 To UBound(lines)
- strMsg = strMsg & " " & lines(j)
- objDictionary.Remove lines(j)
- Next
- strMsg = strMsg & " 行;"
- ' WScript.StdOut.WriteLine strMsg
- ' 设置返回值
- FetchOneNumber = theOne
- End Function
复制代码
|