Java教程

2021-10-16 记录一次面试算法题:语音与流量收费

本文主要是介绍2021-10-16 记录一次面试算法题:语音与流量收费,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

程序输入类似13163351415|1801|200|785|300内容,需计算出客户该月费用。
该段内容表示为:手机号|语音时间(秒)|语音基础费率(每分钟)|流量(MB)|流量费率(每KB)

L = "13163351415|1801|200|785|30".split("|")

def yuyin(a, b):
	# 将语音时间换算为分钟,向上取整
    if a % 60 == 0:
        time = a // 60
    else:
        time = a // 60 + 1
    # 定于语音收费区间及其费率
    qujian = [20, 10, 5, 1, 0]
    feilv = [0.2, 0.5, 1.5, 1, 0]
    sum = 0
    # 通过从后往前切割,将语音时间按语音收费区间切割,落在对应区间的费率,并进行计算
    for i in range(len(qujian)):
        if time > qujian[i]:
            sum += (time - qujian[i]) * feilv[i] * b / 10
            time -= time - qujian[i]
    return int(sum)

def liuliang(a, b):
    qujian = [625, 325, 125, 0]
    LL = [500, 200, 100, 0]
    sum = 0
    if a < 625:
        for i in range(len(qujian)):
            if a > qujian[i]:
                sum += ((a - qujian[i]) + LL[i])
                break
    else:
        shengyuliuliang = a - qujian[0]
        if shengyuliuliang % 100 == 0:
            sum = 500 + (shengyuliuliang // 100) / 2 * 100 + shengyuliuliang % 100
        else:
            sum = 500 + ((shengyuliuliang // 100) / 2 + 1) * 100
    # 流量费用=流量(MB)*1024KB / 10KB(以每10KB收费)* 每10KB费用(厘)/ 10
    liuliangfeiyong = sum * 1024 / 10 * b / 10
    return int(liuliangfeiyong)

yuyinfeiyong = yuyin(int(L[1]), int(L[2]))
liuliangfeiyong = liuliang(int(L[3]),int(L[4]))
print(yuyinfeiyong + liuliangfeiyong)
这篇关于2021-10-16 记录一次面试算法题:语音与流量收费的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!