和地里的题一样,三道题,不计时,都是字符串处理的题,和算法没啥关系,第一题可以选择语言,奇怪的是后面两道题不能选择语言把前两题的代码贴过来了,第三题忘记copy啦,不过都是参考地里的一个帖子写的~~
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string line;
while (getline(cin, line)) {
istringstream in(line);
string temp;
//get the price and pay amount form the line string
getline(in,temp,'';'');
double price = stod(temp);
in>>temp;
double pay = stod(temp);
if(price == pay) cout<<"ZERO"<<endl;
else if(price > pay) cout<<"ERROR"<<endl;
else
{
string res = helper(pay-price);
cout<< res<<endl;
}
}
}
// get the str of the cash register
string helper(double diff)
{
string res = "";
if(diff==0) return res;
double money[12] = {100,50,20,10,5,2,1,0.5,0.25,0.1,0.05,0.01};
string str[12] = {"ONE HUNDRED", "FIFTY", "TWENTY","TEN","FIVE","TWO","ONE","HALF DOLLAR","QUARTER","DIME","NICKEL","PENNY"};
int num[12]={0};
for(int i=0; i<12; i++)
{
num[i] = (double)(diff/money[i]);
if(num[i] > 0)
{
diff -= num[i] * money[i];
if(res != "") res += ",";
res += (num[i]==1 ? "": num[i]+" ")+ str[i];
}
}
return res;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class Main {
/**
* Iterate through each line of input.
*/
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 amount = Double.parseDouble(strs[0]);
double year = Double.parseDouble(strs[1]);
double rate = Double.parseDouble(strs[2]);
double payment = Double.parseDouble(strs[3]);
helper(amount,year,rate,payment);
}
}
public static void helper(double amount, double year, double rate, double payment)
{
amount -= payment;
double mP = (rate * amount / 1200) / (1 - Math.pow((1 + rate / 1200), -1 * year * 12));
System.out.print("$" + (double) Math.round(mP * 100) / 100 + "~");
System.out.println("$" + (int) (Math.rint(mP * year * 12 - amount)));
}
}