可能是很简单的算法问题,来拿分吧!!

ysb 2001-08-20 01:11:55
手头有一个安排机器的程序,算法挺奇怪的,每天按照20个小时排(00:00:00-20:00:00。
例如当前排到:2001-08-20 13:00:00,接下来要排30个小时的机时,结束时间应该是:
2001-08-22 03:00:00,
请问这个算法应该如何写?
救救我吧,阿门!
...全文
113 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lanren_me 2001-08-21
  • 打赏
  • 举报
回复
up
ysb 2001-08-21
  • 打赏
  • 举报
回复
对了,哈哈!!
看看我写的,在SQL StoreProcedure里面
CREATE PROCEDURE Calc_Time
@InBegin as datetime, --开始时间
@InHour as numeric(9,1), --小时数
@OutEnd as datetime output --结束时间
AS

declare @Hour1 as numeric(9,1) --当天尚余的工时
declare @Hour2 as numeric(9,1) --计算整除20余下的工时
declare @IDay as int
declare @Temp as datetime

select @Hour1=datediff(mi,right(@InBegin,8),'20:00:00')/60
if @InHour<@Hour1 --超出当天,按照每天20小时计算
select @OutEnd=dateadd(hh,@InHour,@InBegin)
else
begin
select @Temp=dateadd(hh,@Hour1+4,@InBegin)
select @IDay=(@InHour-@Hour1)/20
select @Temp=dateadd(dd,@IDay,@Temp)
select @Hour2= (@InHour-@Hour1)-@IDay*20
select @OutEnd=dateadd(hh,@Hour2,@Temp)
end
KingSunSha 2001-08-20
  • 打赏
  • 举报
回复
'the result is same
'it seems to be much understandable to use function 'DATEADD'
'pls try again
Public Function GetEndTime(StartTime As Date, WorkingHours As Integer) As Date
Const StartTimePerDay = "00:00:00"
Const EndTimePerDay = "20:00:00"

Dim HoursPerDay As Integer, StartDate As Date, tmp As Integer
HoursPerDay = DateDiff("H", StartTimePerDay, EndTimePerDay)
StartDate = CDate(Format(StartTime, "DD-MMM-YYYY"))
StartTime = CDate(Format(StartTime, "HH:MM:SS"))

tmp = DateDiff("H", StartTime, EndTimePerDay)
If WorkingHours <= tmp Then
GetEndTime = DateAdd("h", WorkingHours, StartDate + StartTime)
Else
StartDate = StartDate + 1 + Int((WorkingHours - tmp) / HoursPerDay)
StartTime = DateAdd("h", (WorkingHours - tmp) Mod HoursPerDay, CDate(StartTimePerDay))
GetEndTime = StartDate + StartTime
End If

End Function
ysb 2001-08-20
  • 打赏
  • 举报
回复
不是,我想要一个算法
Exp("2001-08-22 03:00:00",30)="2001-08-03 03:00:00"
关键问题是Exp是什么?
KingSunSha 2001-08-20
  • 打赏
  • 举报
回复
Public Function GetEndTime(StartTime As Date, WorkingHours As Integer) As Date
Const StartTimePerDay = "00:00:00"
Const EndTimePerDay = "20:00:00"

Dim HoursPerDay As Integer, StartDate As Date, tmp As Integer
HoursPerDay = DateDiff("H", StartTimePerDay, EndTimePerDay)
StartDate = CDate(Format(StartTime, "DD-MMM-YYYY"))
StartTime = CDate(Format(StartTime, "HH:MM:SS"))

tmp = DateDiff("H", StartTime, EndTimePerDay)
If WorkingHours <= tmp Then
GetEndTime = StartDate + StartTime + WorkingHours / 24
Else
StartDate = StartDate + 1 + Int((WorkingHours - tmp) / HoursPerDay)
StartTime = CDate(StartTimePerDay) + (((WorkingHours - tmp) Mod HoursPerDay) / 24)
GetEndTime = StartDate + StartTime
End If

End Function


test result:
?getendtime("2001-08-20 13:00:00",30)
8/22/01 3:00:00 AM
ozw 2001-08-20
  • 打赏
  • 举报
回复
在窗体上放一个List1,

Private Sub Form_Load()
Dim d As Date
Dim i As Integer
d = "2001-08-22 03:00:00"
For i = 1 To 30
d = DateAdd("h", 1, d)
List1.AddItem d
Next i
End Sub
看看是不是你想要的
Ninputer 2001-08-20
  • 打赏
  • 举报
回复
没看懂
ysb 2001-08-20
  • 打赏
  • 举报
回复
怎么连一个人回复都没有????

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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