【题目】实现开机排名的小程序


/**
 * @description
 * @author vermouth.Mac
 * @version 2018年2月4日 下午9:07:36
 * 
 *          源于和搜狐视频的面试。当时答的不好,在这里实施一遍。 如何通过输入一个开机时间,来确定你击败了全国百分之多少的用户。
 * 
 *          此版本的程序缺陷在于,程序关闭后数据就清0了。
 * 
 *          两个注意点
 *           1. 开机超越 = (比我慢的所有人/总人数)*100% 那么问题就来了,第一个key值的所有人都是超越了0%
 *              这个符合逻辑么 
 *          2. (double)5/10 这样才能得到0.5
 * 
 *          3. 解决措施是,当key的种类为一的时候,不按算法来 return 100%
 */

public class KaiJiRank {

    private static int sum = 0;
    private static int keyCount = 0;
    private static HashMap<Integer, Integer> tree = new HashMap<Integer, Integer>();

    public static void main(String[] args) {
        BufferedReader br = null;
        br = new BufferedReader(new InputStreamReader(System.in));
        try {
            String val = "";
            while (true) {
                System.out.println("请输入您的开机时间:\t");
                val = br.readLine();
                if (val.equals("bye"))
                    break;
                else if (val.equals("")) {
                    System.out.println("输入为空,请重新输入");
                    continue;
                }
                System.out.println("您的开机时间是:\t" + val);
                double rate = 0;
                rate = getRate(processString2Int(val));
                String result = String.format("%.2f", rate);
                System.out.println("您的开机速度击败了全国 " + result + "%的用户。");
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                br = null;
            }
        }
    }

    /**
     * 0. 设置key=0 ,记录总人数。 每次开机都++ int sum 1. 根据key getValue 该key的count++ 2. 遍历全部
     * key > m 的所有count值。v 3. 开机超越比例为 v/sum *100 返回
     **/

    public static double getRate(int m) {
        int outer = 0; // 落后的人
        double rate = 0;
        sum++; // 0. 总数加一
        if (tree.containsKey(m)) {
            tree.put(m, tree.get(m) + 1);
        } else {
            tree.put(m, 1);
            keyCount++;
        }

        if (keyCount>1) {
            Iterator<Map.Entry<Integer, Integer>> it = tree.entrySet().iterator();
            w: while (it.hasNext()) {
                Map.Entry<Integer, Integer> map = it.next();
                Integer key = map.getKey();
                Integer count = map.getValue();
                if (key <= m) {
                    continue w;
                } else {
                    outer += count;
                }
            }
            rate = 100 * ((double) outer / sum);
            return rate;
        }else
            return 100;

    }

    /***
     * 专注于处理输入的字符串转为int的操作。 11.5 -> 11
     * 
     * @param raw
     * @return
     */
    public static int processString2Int(String raw) {
        Double d = Math.floor(Double.valueOf(raw));
        int result = d.intValue();
        return result;
    }

}




至于当只有一个时间的时候,到底是超过100%的用户还是超过0%的用户…

我个人还是倾向于0%

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值