ruby - 算法中的无限循环以匹配以不同速度运行的时钟

标签 ruby algorithm

我正在尝试解决这个问题:

Two clocks, which show the time in hours and minutes using the 24 hour clock, are running at different speeds. Each clock is an exact number of minutes per hour fast. Both clocks start showing the same time (00:00) and are checked regularly every hour (starting after one hour) according to an accurate timekeeper. What time will the two clocks show on the first occasion when they are checked and show the same time? NB: For this question we only care about the clocks matching when they are checked. For example, suppose the first clock runs 1 minute fast (per hour) and the second clock runs 31 minutes fast (per hour). • When the clocks are first checked after one hour, the first clock will show 01:01 and the second clock will show 01:31; • When the clocks are checked after two hours, they will show 02:02 and 03:02; • After 48 hours the clocks will both show 00:48.

这是我的代码:

def add_delay(min,hash)
    hash[:minutes] = (hash[:minutes] +  min)
    if hash[:minutes] > 59 
        hash[:minutes] %= 60
        if min < 60 
            add_hour(hash)
        end
    end

    hash[:hour] += (min / 60) 
    hash
end

def add_hour(hash) 
    hash[:hour] += 1
    if hash[:hour] > 23
        hash[:hour] %= 24
    end
    hash
end

def compare(hash1,hash2)
    (hash1[:hour] == hash2[:hour]) && (hash1[:minutes] == hash2[:minutes])
end




#-------------------------------------------------------------------
first_clock = Integer(gets) rescue nil
second_clock = Integer(gets) rescue nil



#hash1 = if first_clock < 60 then {:hour => 1,:minutes => first_clock} else {:hour => 1 + (first_clock/60),:minutes => (first_clock%60)} end
#hash2 = if second_clock < 60 then {:hour => 1,:minutes => second_clock} else {:hour => 1 + (second_clock/60),:minutes => (second_clock%60)} end

hash1 = {:hour => 0, :minutes => 0}
hash2 = {:hour => 0, :minutes => 0}

begin 
    hash1 = add_hour(hash1)
    hash1 = add_delay(first_clock,hash1)
    hash2 = add_hour(hash2)
    p hash2.to_s
    hash2 = add_delay(second_clock,hash2)
    p hash2.to_s
end while !compare(hash1,hash2)

#making sure print is good
if hash1[:hour] > 9

    if hash1[:minutes] > 9
        puts hash1[:hour].to_s + ":" + hash1[:minutes].to_s
    else
        puts hash1[:hour].to_s + ":0" + hash1[:minutes].to_s
    end
else
    if hash1[:minutes] > 9
        puts "0" + hash1[:hour].to_s + ":" + hash1[:minutes].to_s
    else
        puts "0" + hash1[:hour].to_s + ":0" + hash1[:minutes].to_s
    end

end

#-------------------------------------------------------------------

对于 1 和 31,代码按预期运行。对于任何更大的值,例如 5 和 100,它似乎进入了一个无限循环,我看不出错误在哪里。出了什么问题?

最佳答案

add_delay 函数中的逻辑有缺陷。

def add_delay(min,hash)
    hash[:minutes] = (hash[:minutes] +  min)
    if hash[:minutes] > 59 
        hash[:minutes] %= 60
        if min < 60 
            add_hour(hash)
        end
    end

    hash[:hour] += (min / 60) 
    hash
end

如果 hash[:minutes] 大于 60,无论如何都应该增加小时数。请注意,小于 60 的增量会导致分钟数溢出。

此外,如果增量超过 60 分钟,您可能不得不多次增加小时数。

最后,做hash[:hour] += (min/60)是错误的,因为min不一定超过60,因为你已经做了add_hour(hash).

这是该函数的更正版本:

def add_delay(minutes, time)
    time[:minutes] += minutes
    while time[:minutes] > 59  # If the minutes overflow,
        time[:minutes] -= 60   # subtract 60 minutes and
        add_hour(time)         # increment the hour.
    end                        # Repeat as necessary.
    time
end

您可以将此功能插入到现有代码中。我只是冒昧地将函数内的 min 重命名为 minutes 并将 hash 重命名为 time

关于ruby - 算法中的无限循环以匹配以不同速度运行的时钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32671592/

相关文章:

algorithm - 最优算法可以不完整吗?

python - 加快 groupby.apply pandas 匹配

ruby-on-rails - 如何在 rails 中使用 like 子句查询?

ruby - 您可以指定用于 Sinatra 重定向的 HTTP 方法吗?

Ruby 模板 if else 语句

algorithm - 如何随机选择地球表面的一个点?

File.read 的 Ruby 性能

ruby-on-rails - 'rake db:seed' 能告诉你它阻塞在哪条记录上吗?

组织矩阵以使邻居最接近的算法

algorithm - 有没有办法对分区的 spark 数据集并行运行操作?