注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
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")){
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]);
}
}
}
|