算法小抄12

 

package test;

import java.util.HashMap;

/**
 * @author 欧阳
 * @date 2022/3/18
 */
public class Demo19 {

    public static void main(String[] args) {
        Integer distance = distance("B4", "A6");
        System.out.println(distance);
        System.out.println(money(distance));
    }

    public static double money(Integer distance){
        if(distance <= 3 & distance > 0){
            return 2.0;
        }
        if(distance > 3 & distance <= 7){
            return 2 + (distance - 3) * 1.0;
        }
        if(distance > 7){
            return 6 + (distance - 7) * 0.5;
        }
        return 0;
    }

    public static Integer distance(String A , String B){
        HashMap<String, Integer> map = new HashMap<>();
        map.put("B1", -7);map.put("B2", -4);map.put("B3", 0);map.put("B4", 2);map.put("B5", 3);map.put("B6", 4);map.put("B7", 7);map.put("B8", 9);map.put("B9", 14);
        map.put("B10", 15);map.put("B11", 18);map.put("B12", 20);map.put("A1", 13);map.put("A2", 11);map.put("A3", 8);map.put("A4", 2);map.put("A5", 0);map.put("A6", -2);map.put("A7", -5);map.put("A8", -6);map.put("A9", -15);
        Integer a = map.get(A);
        Integer b = map.get(B);
        if(A.startsWith("A") & B.startsWith("A") || A.startsWith("B") & B.startsWith("B")){
            if((a > 0 & b > 0)){
                return (a - b) > 0 ? a - b : b - a;
            }
            else if(a < 0 & b < 0){
                return (a < b) ? -a - -b : -b - -a;
            }else {
                return a < 0 ? b - a : a - b;
            }
        }
        if(a < 0 & b < 0){
            return -a + -b;
        }else {
            return a < 0 ? b - a : a - b;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值