回复: 9
跳转到指定楼层
上一主题 下一主题
收起左侧

贴一个blackrock oa 的答案

🔗
匿名用户-SJFU0  2018-12-11 02:59:58 |倒序浏览

2018(10-12月) 码农类General 硕士 实习@blackrock - 网上海投 - 在线笔试  | | Other | 应届毕业生

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
话不多说上答案:
第一题:
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.*;

public class getchange {
    /*编程挑战说明:
        The goal of this challenge is to design a cash register program. You will be given two decimal numbers. The first is the purchase price (PP) of the item. The second is the cash (CH) given by the customer. Your register currently has the following bills/coins within it:
        'PENNY': .01,
        'NICKEL': .05,
        'DIME': .10,
        'QUARTER': .25,
        'HALF DOLLAR': .50,
        'ONE': 1.00,
        'TWO': 2.00,
        'FIVE': 5.00,
        'TEN': 10.00,
        'TWENTY': 20.00,
        'FIFTY': 50.00,
        'ONE HUNDRED': 100.00
        The aim of the program is to calculate the change that has to be returned to the customer.
        输入:
        Your program should read lines of text from standard input. Each line contains two numbers which are separated by a semicolon. The first is the Purchase price (PP) and the second is the cash(CH) given by the customer.
        输出:
        For each line of input print a single line to standard output which is the change to be returned to the customer. In case the CH < PP, print out ERROR. If CH == PP, print out ZERO. For all other cases print the amount that needs to be returned, in terms of the currency values provided. The output should be alphabetically sorted.
        Test 1
        测试输入 下载测试输入15.94;16.00
        预期输出 下载测试输出NICKEL,PENNY
        Test 2
        测试输入 下载测试输入17;16
        预期输出 下载测试输出ERROR
        Test 3
        测试输入 下载测试输入35;35
        预期输出 下载测试输出ZERO
        Test 4
        测试输入 下载测试输入45;50
        预期输出 下载测试输出FIVE
        */

    public static void main(String[] args) throws IOException {

        InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
        BufferedReader in = new BufferedReader(reader);
        String line;
        while ((line = in.readLine()) != null) {
            String[] strs = line.split(";");
            double price = Double.parseDouble(strs[0]);
            double pay = Double.parseDouble(strs[1]);
            if(price > pay) {
                System.out.println("ERROR");
            }else if(price == pay) {
                System.out.println("ZERO");
            }else {
                HashMap<String,Integer>map=new HashMap<>();
                 helper(pay - price,map);
                 String res="";
               // if(res.charAt(res.length() - 1) == ',') {
                   // res = res.substring(0, res.length() - 1);
                   // String[] r=res.split(",");
               // }

                //for (String keys :map.keySet())
                List<String> sortedKeys=new ArrayList(map.keySet());
                Collections.sort(sortedKeys);
                for (String key : sortedKeys){
                    int v=map.get(key);
                    if (v==1) res+=key+",";
                    else res+=v+" "+key+",";
                }
                if(res.charAt(res.length() - 1) == ',') {
                    res = res.substring(0, res.length() - 1);

                }
                System.out.println(res);
            }
        }
    }

    public static void helper(double diff,HashMap<String,Integer>map) {
        //DecimalFormat df = new DecimalFormat("###.###");
        diff=roundoff(diff);
        if(diff == 0) {
            return;
        }
        //if(diff ==0.01) return "PENNY";
        else if(diff < 0.05) {
            int num = (int) (diff / 0.01);
            //System.out.println(diff);
            if (num==0)return;
            //return (num == 1 ? "" : num + " ") + "PENNY";
            if (map.containsKey("PENNY")){
               
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
quot;,info[0],""+(has-need)});
                // }
                map.put(info[0],new Double[]{portasset,portprice,portshares});
            }
            for (String key: map.keySet()){
                Double[] val=map.get(key);
                val[0]=val[0]/totalassest;
                map.put(key,val);
            }

            for (String benchwork:benchmark){
                String[] info =benchwork.split(",");
                String type=info[1];
                double benchasset=0.0;
                double benchprice=Double.parseDouble(info[3]);
                double benchshares=Double.parseDouble(info[2]);
                double benchinterest=Double.parseDouble(info[4]);
                if (type.equals("STOCK")){
                    benchasset =benchprice*benchshares;
                }else if (type.equals("BOND")){
                    benchasset = benchshares*(benchprice+benchinterest)*0.01;
                }
                //if(need>has){
                //   res.add(new String[] {"BUY",info[0],""+(need-has)});
                // }else if (need<has) {
                //   res.add(new String[]{"SELL",info[0],""+(has-need)});
                // }
                //map2.put(info[0],benchasset/totalassest);
                double benchpercnetage=benchasset/totalassest;
                if (map.containsKey(info[0])){
                    Double[] val=map.get(info);
                    System.out.println(val[0]);
                    if (benchpercnetage>val[0]){
                        double extrashares=(benchpercnetage-val[0])*totalassest/val[1];
                        res.add(new String[]{"BUY",info[0],""+extrashares});
                    }else if (benchpercnetage<val[0]){
                        double extrashares=(val[0]-benchpercnetage)*totalassest/val[1];
                        res.add(new String[]{"SELL",info[0],""+extrashares});
                    }
                }
            }


            Collections.sort(res,new Comparator<String[]>(){
                @Override
                public int compare(String[] l1, String[] l2){
                    return l1[1].compareTo(l2[1]);
                }
            });
            for (String[] strs:res){
                System.out.println(strs[0]+","+strs[1]+","+strs[2]);
            }

        }
    }



评分

参与人数 6大米 +64 收起 理由
黄曼巴爸爸 + 2 很有用的信息!
杨超越 + 3 给你点个赞!
admin + 50
294413979 + 3 很有用的信息!
bailongma66 + 3 很有用的信息!

查看全部评分


上一篇:报一个千眼一面挂经
下一篇:18FA感受面经合集 Waymo DeepScale TuSimple JingChi Apple Citadel Akuna Sift
🔗
bailongma66 2019-1-2 15:59:59 | 只看该作者
全局:
谢谢楼主,已加米,请问这个title是2018 BlackRock APG Java Test (New York)吗?
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-SJFU0  2019-1-3 09:43:00
bailongma66 发表于 2019-1-2 15:59
谢谢楼主,已加米,请问这个title是2018 BlackRock APG Java Test (New York)吗?

额 我不记得了。。。
回复

使用道具 举报

🔗
bailongma66 2019-1-6 07:59:31 | 只看该作者
全局:
论坛匿名用户 发表于 2019-1-3 09:43
额 我不记得了。。。

啊,就是发来OA的链接上写的..
回复

使用道具 举报

🔗
qingtingj 2019-1-6 08:14:16 | 只看该作者
全局:
谢谢lz,祝lz offer多多
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-SJFU0  2019-1-6 10:55:16
bailongma66 发表于 2019-1-6 07:59
啊,就是发来OA的链接上写的..

看了就是 software engineering
回复

使用道具 举报

🔗
bailongma66 2019-1-7 07:01:49 | 只看该作者
全局:
论坛匿名用户 发表于 2019-1-6 10:55
看了就是 software engineering

麻烦了,谢谢楼主!
回复

使用道具 举报

🔗
294413979 2019-1-9 02:15:38 | 只看该作者
全局:
楼主太优秀了,已加米,OA过了吗?还有后续吗?
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-SJFU0  2019-1-10 13:38:13
294413979 发表于 2019-1-9 02:15
楼主太优秀了,已加米,OA过了吗?还有后续吗?

至今无消息
回复

使用道具 举报

🔗
yyyvetteee 2019-2-19 11:25:49 | 只看该作者
全局:
楼主厉害了!谢谢楼主!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表