package code_03_chapter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
*Created by William on 2018/6/18 0018
*/
public class QuestionsInChapter3 {
/**
* Q1:
* 本书行将出版之时,美国的个人所得税分为5种不同的费率,其中最大的费率大约为40%。
* 以前的情况更为复杂,税率也更高。下面所示的程序文本采用25个if语句的合理方式来计算1978年的美国联邦所得税。
* 税率分别为0.14,0.15,0.16, 0.17, 0.18,·····。此后的费率增幅大于0.01.有何建议?
* 两种解法,Q1_1和Q1_2
* Q1_1是硬代码,很难看懂
* 推荐Q1_2,效率也高
*/
public static class Q1_1 {
public static double taxPay(double salary) {
double tax = 0;
double tempI;
double tempJ;
int round = 0;
DecimalFormat df = new java.text.DecimalFormat("#.00");
for (double i = 0.14; i < 0.41; i += 0.01) {
tempI = Double.parseDouble(df.format(i));
for (double j = 2200; j < 2200 + 13500; j += 500) {
int temp = (int) ((j - 2200) / 500) + 1;
tempJ = Double.parseDouble(df.format(temp * 0.01 + 0.13));
if (salary >= j && tempI == tempJ && salary - 2200 > 0) {
double taxPayNumber = salary - 2200 - 500 * round;
if (taxPayNumber >= 500) taxPayNumber = 500;
tax += taxPayNumber * tempI;
}
}
round++;
}
return Double.parseDouble(df.format(tax));
}
public static void main(String[] args) {
System.out.println(taxPay(2200));
System.out.println(taxPay(2700));
System.out.println(taxPay(3200));
System.out.println(taxPay(3700));
}
}
public static class Q1_2 {
static class Tax {
double rate; //税收率下界
double base; //基本税收
double bound; //税收下界
}
Tax[] taxTable = new Tax[1000];
public void createTable(int n) {
for (int i = 0; i < n; i++) {
taxTable[i] = new Tax();
}
taxTable[0].rate = 0;
taxTable[0].bound = 0;
taxTable[0].base = 0;
taxTable[1].rate = 0.14;
taxTable[1].bound = 2200;
taxTable[1].base = 0;
for (int i = 2; i < n; i++) {
taxTable[i].bound = taxTable[i - 1].bound + 500;
taxTable[i].rate = taxTable[i - 1].rate + 0.01;
taxTable[i].base = taxTable[i - 1].base + taxTable[i - 1].rate * 500;
}
}
public double calculateTax(double salary) {
int index = (int) ((salary - 2200 + 500 - 1) / 500);
return taxTable[index].base + taxTable[index].rate * (salary - taxTable[index].bound);
}
public static void main(String[] args) {
Q1_2 q1_2 = new Q1_2();
q1_2.createTable(25);
System.out.println(q1_2.calculateTax(2500));
}
}
/**
* Q3:
* 编写一个“banner”函数,该函数的输入为大写字母,输出为一个字符数组,该数组以图形化的方式表示该字母。
* 感觉挺无聊,Q2,Q3不做了
*/
/**
* Q4:
* 1.编写处理如下日期的函数:给定两个日期,计算两者之间的天数;
* 2.给定一个日期,返回值为周几;
* 给定月和年,使用字符数组生成该月的日历。
* 这个题用java实在简单
*/
public static class Q4 {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public int dayCount(String date1, String date2) throws ParseException {
Date parse1 = dateFormat.parse(date1);
Date parse2 = dateFormat.parse(date2);
long l = Math.abs(parse1.getTime() - parse2.getTime());
return (int) (l / 86400000);
}
public void dateFormat(String date) throws ParseException {
Date date1 = dateFormat.parse(date + "是:");
System.out.println(date);
dateFormat.applyPattern("D");
System.out.println("一年中的第几天:" + dateFormat.format(date1));
dateFormat.applyPattern("d");
System.out.println("一个月中的第几天:" + dateFormat.format(date1));
dateFormat.applyPattern("w");
System.out.println("一年中的第几周:" + dateFormat.format(date1));
dateFormat.applyPattern("W");
System.out.println("一个月中的第几周:" + dateFormat.format(date1));
dateFormat.applyPattern("E");
System.out.println("一个星期中的天数:" + dateFormat.format(date1));
}
public static void main(String[] args) throws ParseException {
Q4 q4 = new Q4();
System.out.println(q4.dayCount("2018-02-11", "2018-02-15"));
q4.dateFormat("2018-02-11");
}
}
/**
* Q5:将输入的单词表示成带有后缀连字符的单词
*/
public static class Q5 {
static String[] table = {"et-ic", "al-is-tic", "s-tic", "p-tic", "-lyt-ic", "ot-ic", "an-tic",
"n-tic", "c-tic", "at-ic", "h-nic", "n-ic", "m-ic", "l-lic", "b-lic", "-clic", "l-ic",
"h-ic", "f-ic", "d-ic", "-bic", "a-ic", "-mac", "i-ac"};
public static void wordMatch(String inputWord) {
String[] newTable = new String[table.length];
for (int i = 0; i < table.length; i++) {
newTable[i] = table[i].replace("-", "");
}
for (int i = 0; i < newTable.length; i++) {
if (inputWord.contains(newTable[i])) {
System.out.println(inputWord.replace(newTable[i],table[i]));
}
}
}
public static void main(String[] args) {
wordMatch("fucketic");
}
}
}
算法------编程珠玑(ProgrammingPeals)第三章习题(JAVA)
最新推荐文章于 2022-05-13 17:23:38 发布