【每日一题0912】度数转换器

:woman_mage:已知度数是一个非负浮点数,并且满足:1度= 1小时,1分钟=60秒钟。请编写一个函数convert,接收一个度数值degree,以列表的格式返回它所表示的度数、分钟数和秒数。
备注:所有的度数/分钟数、秒数均取整数部分。
示例:
输入:50,输出:[50]
输入:40.567,输出[40, 34, 1]。因为0.567度是34.02分钟,取34;0.02分钟是1.2,取1。

题目难度:简单
题目来源:codewars

def convert(degree: int) -> list:
    pass

assert convert(50) == [50, 0, 0]
assert convert(80.5) == [80, 30 ,0]
assert convert(40.567) == [40, 34, 1]
# 方法一
def convert(degree):
    list=[]
    str_degress=str(degree)
    if str_degress.count(".")==0:
        list=[degree,0,0]
        return list
    else:
        a=str(degree).split(".")
        length=len(a[1])
        list.append(a[0])
        # 求分
        m=round(int(a[1])/10**length*60,2)
        b=str(m).split(".")
        list.append(b[0])
        # 求秒
        s=int(int(b[1])/10**len(b)*60)
        list.append(s)
        return list
# 方法二
import math
def convert1(degree):
    list=[]
    list1=math.modf(degree)
    list.append(int(list1[1]))
    a=list1[0]*60
    list2=math.modf(a)
    list.append(int(list2[1]))
    s=int(list2[0]*60)
    list.append(s)
    return list
def convert(degree: int) -> list:
    if degree==int(degree):
        return [int(degree),0,0]
    s = (degree-int(degree))*60
    if s == int(s):
        return [int(degree),int(s),0]
    return [int(degree),int(s),int((s-int(s))*60)]
def convert(degree) -> list:
    min = (degree - int(degree)) * 60
    second = (min - int(min)) * 60
    return [int(degree),int(min),int(second)]
 public ArrayList<Integer> convert(Double d){
       ArrayList<Integer> result = new ArrayList<>();
       BigDecimal du = new BigDecimal(d.toString());
       result.add(d.intValue());
       BigDecimal subtract = du.subtract(new BigDecimal(d.intValue()));
       int fen = subtract.multiply(new BigDecimal(60)).intValue();
       result.add(fen);
       BigDecimal subtract1 = subtract.multiply(new BigDecimal(60)).subtract(new BigDecimal(fen));
       int miao = subtract1.multiply(new BigDecimal(60)).intValue();
       result.add(miao);
       return result;
   }

   @Test
   public void TestConvert(){
       assert convert(50.0).equals(Arrays.asList(50,0,0));
       assert convert(80.5).equals(Arrays.asList(80,30,0));
       assert convert(40.567).equals(Arrays.asList(40,34,1));
   }

def convert(degree: float) -> list:
  a = degree - int(degree)
  b = (a - int(a)) * 60 if a > 0 else 0
  c = (b - int(b)) * 60 if b > 0 else 0

  return [int(degree),int(b),int(c)]

assert convert(50) == [50, 0, 0]
assert convert(80.5) == [80, 30 ,0]
assert convert(40.567) == [40, 34, 1]
关闭