请教达人一个问题,万分感谢!

yangcheng12075 2021-12-07 14:17:17



期末了,朋友孩子的大学计算机老师出了一道基础题。求助论坛的达人,希望能有完美的解决方案。

题目如下:

输出1000年至9999年所有的对称日,用VBA实现。

(所谓的对称日,就是无论您从左往右念,还是从右往左念,它都是一样的数字!比如:20211202)

...全文
171 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2023-05-25
  • 打赏
  • 举报
回复

仅作参考:


Sub FindSymmetricDates()
    Dim year As Integer
    Dim month As Integer
    Dim day As Integer
    Dim dateStr As String
    
    ' 循环遍历 1000 年至 9999 年的所有日期
    For year = 1000 To 9999
        For month = 1 To 12
            For day = 1 To 31
                ' 检查日期是否有效
                If IsValidDate(year, month, day) Then
                    ' 将日期转换为字符串
                    dateStr = FormatDate(year, month, day)
                    
                    ' 检查日期是否为对称日
                    If IsSymmetric(dateStr) Then
                        ' 输出对称日
                        Debug.Print dateStr
                    End If
                End If
            Next day
        Next month
    Next year
End Sub

Function IsValidDate(year As Integer, month As Integer, day As Integer) As Boolean
    ' 使用 IsDate 函数检查日期是否有效
    IsValidDate = IsDate(month & "/" & day & "/" & year)
End Function

Function FormatDate(year As Integer, month As Integer, day As Integer) As String
    ' 格式化日期为字符串,例如:20211202
    FormatDate = Format(year, "0000") & Format(month, "00") & Format(day, "00")
End Function

Function IsSymmetric(dateStr As String) As Boolean
    ' 检查日期是否为对称日
    Dim i As Integer
    Dim lenStr As Integer
    
    lenStr = Len(dateStr)
    
    For i = 1 To lenStr \ 2
        If Mid(dateStr, i, 1) <> Mid(dateStr, lenStr - i + 1, 1) Then
            IsSymmetric = False
            Exit Function
        End If
    Next i
    
    IsSymmetric = True
End Function

在 VBA 中,可以将上述代码放置在一个 VBA 模块中,并执行 FindSymmetricDates 过程即可输出所有的对称日。输出结果将显示在 VBA 的调试窗口中 (按下 Ctrl + G 打开调试窗口)。请注意,由于计算所有日期的对称性可能需要一些时间,建议等待一段时间以获取完整的输出结果

薛定谔_51 2023-05-25
  • 打赏
  • 举报
回复

突然发现有个内置函数就能实现

Sub 回文()
    Dim s As Date, e As Date, t$, r&
    s = #1/1/1000#: e = #12/31/9999#
    For i = s To e
        t = Format(i, "yyyymmdd")
        If t = StrReverse(t) Then r = r + 1: Cells(r, "a").Value = t
    Next
End Sub
薛定谔_51 2022-09-20
  • 打赏
  • 举报
回复

一共331个

Function huiwen(str$)
    Dim c&, m&, i&, ishuiwen As Boolean
    c = Len(str)
    m = WorksheetFunction.RoundUp(c / 2, 0)
    ishuiwen = True
    For i = 1 To m
        t1 = Mid(str, i, 1): t2 = Mid(str, c + 1 - i, 1)
        If t1 <> t2 Then ishuiwen = False: Exit For
    Next
    If ishuiwen = True Then
        huiwen = 1
    Else
        huiwen = 0
    End If
End Function

Sub 测试()
    Dim s As Date, e As Date, t$, w, r&
    s = #1/1/1000#: e = #12/31/9999#
    For i = s To e
        t = Format(i, "yyyymmdd")
        w = huiwen(t)
        If w = 1 Then r = r + 1: Cells(r, "a").Value = t
    Next
End Sub
荭谚 2021-12-26
  • 打赏
  • 举报
回复

咋没有人回答呢, 我也来 学习一下

2,462

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧