ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
12
返回列表 发新帖
楼主: northwolves

[Excel 程序开发] [第17期] 乘方[已总结]

[复制链接]

TA的精华主题

TA的得分主题

发表于 2006-11-20 23:40 | 显示全部楼层

楼主的方法算出来同我的有些差别,很大的数会差2,用下面的例子算会差1。用我的方法是6146,楼主的方法是6145。我是用下面的方法把所有的全列出来验证的。

Function powercount3(dinput As Double) As Double
    Dim i As Double
    Dim tmp As String
    Dim j As Long
   
    j = 1
    For i = dinput To 2 Step -1
        tmp = maxpower3(i)
        If tmp <> "0=" Then
            i = Mid(tmp, 1, InStr(tmp, "=") - 1)
            Cells(j, 26) = i
            Cells(j, 27) = Mid(tmp, InStr(tmp, "="))
            j = j + 1
        Else
            Exit For
        End If
    Next i
    powercount3 = "OK"
End Function

Function maxpower3(dinput As Double) As String
    Dim tmp As Double
    Dim tmpStr As String
    Dim tmpL As Double
    tmpStr = ""
   
    For i = 2 To dinput
        tmpL = Int(dinput ^ (1 / i))
        If tmpL = 1 Then
            Exit For
        Else
            'when the difference is very small, some strange thing happen
            '------------------------------------------------------------
            If tmpL ^ i > dinput Then
                tmpL = tmpL - 1
            End If
            If (tmpL + 1) ^ i = dinput Then
                tmpL = tmpL + 1
            End If
            '-----------------------------------------------------------
            If tmpL ^ i > tmp Then
                tmp = tmpL ^ i
               
                tmpStr = tmpL & "^" & i
            End If
        End If
    Next i
    maxpower3 = tmp & "=" & tmpStr
End Function
Sub mmm()
    MsgBox powercount3(33666665)
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2006-11-21 00:43 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

总结:

本期题目其实难度不大,关键是要特别细心,一不小心就会无中生有或丢三落四。

大家的解法和思路基本都正确。但参赛者比较少,估计有些朋友被题目的大数字吓倒了。

TA的精华主题

TA的得分主题

发表于 2006-11-21 09:47 | 显示全部楼层

出错的原因是应为vba开方运算" ^ "本身存在误差,vba在计算int(125^(1/3))时计算出的值为4,实际为5。据此我自定义了2个函数来消除误差,附件为修正后的程序,程序速度稍慢。33666665用我修正后的程序计算结果为6141。

 


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?免费注册

x

TA的精华主题

TA的得分主题

发表于 2006-11-21 14:57 | 显示全部楼层

powercount(33666665) 用我的算出来也是6146。

还没研究版主的以及版主推荐的算法,过会儿看看结果的误差发生在哪里。

TA的精华主题

TA的得分主题

 楼主| 发表于 2006-11-21 15:37 | 显示全部楼层
QUOTE:
以下是引用winland在2006-11-20 23:40:40的发言:

楼主的方法算出来同我的有些差别,很大的数会差2,用下面的例子算会差1。用我的方法是6146,楼主的方法是6145。我是用下面的方法把所有的全列出来验证的。

Function powercount3(dinput As Double) As Double
    Dim i As Double
    Dim tmp As String
    Dim j As Long
   
    j = 1
    For i = dinput To 2 Step -1
        tmp = maxpower3(i)
        If tmp <> "0=" Then
            i = Mid(tmp, 1, InStr(tmp, "=") - 1)
            Cells(j, 26) = i
            Cells(j, 27) = Mid(tmp, InStr(tmp, "="))
            j = j + 1
        Else
            Exit For
        End If
    Next i
    powercount3 = "OK"
End Function

Function maxpower3(dinput As Double) As String
    Dim tmp As Double
    Dim tmpStr As String
    Dim tmpL As Double
    tmpStr = ""
   
    For i = 2 To dinput
        tmpL = Int(dinput ^ (1 / i))
        If tmpL = 1 Then
            Exit For
        Else
            'when the difference is very small, some strange thing happen
            '------------------------------------------------------------
            If tmpL ^ i > dinput Then
                tmpL = tmpL - 1
            End If
            If (tmpL + 1) ^ i = dinput Then
                tmpL = tmpL + 1
            End If
            '-----------------------------------------------------------
            If tmpL ^ i > tmp Then
                tmp = tmpL ^ i
               
                tmpStr = tmpL & "^" & i
            End If
        End If
    Next i
    maxpower3 = tmp & "=" & tmpStr
End Function
Sub mmm()
    MsgBox powercount3(33666665)
End Sub

你是对的,看来符点运算是存在很多问题。

TA的精华主题

TA的得分主题

 楼主| 发表于 2006-11-21 17:07 | 显示全部楼层

找见问题所在了:

Private Const p1 As String = "{2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}"
Private Const p2 As String = "{6,10,14,15,21,22,25,26,33,34,35,38,39,46}"


Function powercount(ByVal r As Range) As Long
powercount = Evaluate("SUMPRODUCT(INT(" & r & "^(1/" & p1 & ")))-SUMPRODUCT(INT(" & r & "^(1/" & p2 & ")))")  '前者15个质数,1被计数15次,后者14个质数,1被计数14次,正好抵消
End Function

Private Const p2 As String = "{6,10,14,15,21,22,26,33,34,35,38,39,46}" '1,2的误差发生在2^25,3^25 处,由于指数完全平方数时不会被重复计数,故25不应计数在内


Function powercount(ByVal r As Range) As Long
powercount = Evaluate("SUMPRODUCT(INT(" & r & "^(1/" & p1 & ")))-SUMPRODUCT(INT(" & r & "^(1/" & p2 & ")))") - 1 '前者15个质数,1被计数15次,后者13个质数,1被计数13次,总计多计1次,故减1
End Function

Function maxpower(ByVal r As Range) As String
Dim largest As Double, power As Byte, p3 As String
largest = Evaluate("MAX(INT(" & r & "^(1/" & p1 & "))^" & p1 & ")")
p3 = largest & "^(1/" & p1 & "))"
power = Evaluate("1/Max((INT(" & p3 & "=" & p3 & "*(1/" & p1 & "))")
maxpower = largest & "=" & largest ^ (1 / power) & "^" & power
End Function

点评

能进行理论阐述就更好了  发表于 2014-3-19 19:22

TA的精华主题

TA的得分主题

发表于 2006-11-21 23:11 | 显示全部楼层
QUOTE:
以下是引用fjlhgs在2006-11-21 9:47:26的发言:

出错的原因是应为vba开方运算" ^ "本身存在误差,vba在计算int(125^(1/3))时计算出的值为4,实际为5。据此我自定义了2个函数来消除误差,附件为修正后的程序,程序速度稍慢。33666665用我修正后的程序计算结果为6141。

 


[此贴子已经被作者于2006-11-23 16:58:35编辑过]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?免费注册

x
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-3-29 22:02 , Processed in 0.045972 second(s), 11 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表