在序言中将分钟转换为小时 [英] Converting minutes to hours in prolog

查看:32
本文介绍了在序言中将分钟转换为小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此代码在序言中将总分钟数(N)转换为小时数(H)和分钟数(M)(尚未实现计算分钟数):

I am trying to convert total minutes(N) to number of hours(H) and number of minutes(M) in prolog using this code (Have not implemented counting the minutes yet):

minutes_to_hours(N, H, M) :-

   (   N >= 60
   ->  H is H1+1,
       N is N1-60,
       minutes_to_hours(N, H, M)
   ;   writeln(H)
   ).

我收到此错误:

Arguments are not sufficiently instantiated
In:
[2] _1440 is _1446+1
[1] mins_to_hours_and_mins(60,_1508,_1510) at  line 1

推荐答案

根据您的要求,使用递归的可能解决方案可能是:

As you requested, a possible solution using recursion could be:

minutes_to_hours(Mins,Hours) :-
    (   Mins > 60 ->  
        M is Mins - 60,
        H1 is Hours + 1,
        minutes_to_hours(M,H1) ;
        format('Hours: ~w, Reminder: ~w~n',[Hours,Mins])
    ).

?- minutes_to_hours(125,0).
Hours: 2, Reminder: 5
true

但是,您应该更喜欢带有 mod// 的 @CapelliC 解决方案.

However, you should prefer the solution of @CapelliC with mod and //.

这篇关于在序言中将分钟转换为小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆