我试图在没有条件语句和循环的情况下重写代码。 我的代码是根据这样的指令编写的,但是没有循环和条件语句
一个将输入作为开始月份和日期至结束月份和日期以及
的程序计算总价。
假定输入总是正确的。
输入范围为当年1月1日至12月31日
1月1/1是星期一。
输入开始日期始终为星期一
每个月包含31天,奇数月包含30天
对于工作日价格-> $ 2
星期六-> $ 3
周日-> $ 5
如果客户预订超过50天,价格将稳定在1美元
public class test {
int startMonth;
int startDay;
int endMonth;
int endDay;
int totalDate;
public test (int startMonth, int startDay, int endMonth, int endDay) {
this.endMonth = endMonth;
this.endDay = endDay;
this.startMonth = startMonth;
this.startDay = startDay;
}
public int getPrice() {
getTotalDate();
int price = 0;
int discount = totalDate > 50 ? totalDate - 50 : 0;
System.out.println("discount " + discount);
totalDate = totalDate > 50 ? 50 : totalDate % 50;
System.out.println("totalData : " + totalDate);
int sunDay = totalDate/7;
int satDay = totalDate/7 + (totalDate%7)/6;
int weekDay = totalDate - sunDay - satDay;
price+= sunDay*5 + satDay*3 + weekDay*2;
return price + discount;
}
public int getTotalDate() {
int gapOfMonth = endMonth - startMonth;
totalDate = gapOfMonth*30 + (gapOfMonth +1)/2 + (endDay - startDay);
return totalDate;
}
public static void main (String[] args) {
test t = new test(1,1,2,30);
System.out.println("test");
System.out.println(t.getPrice());
}
}
答案 0 :(得分:2)
无需三元即可完成
public int getPrice(int totalDay) {
int totalPrice = 0;
int difference = totalDay-50;
//from https://stackoverflow.com/a/2707438/529282
int absDifference = difference*(1-2*((3*difference)/(3*difference+1)));
//this essentially gives the minimum value between totalDay and 50
int before50 = (totalDay+50-absDifference)/2;
int after50 = totalDay-before50;
totalPrice += after50;
//the before 50 is where the complex calculation is needed
int before50 = totalDay - after50;
//first, the base price for weekday
totalPrice += before50 * 2;
//then we add the whole week difference (sat+sun price - weekday price)
totalPrice += (before50 / 7) * 4;
//the we add the stray saturday if any
totalPrice += (before50 % 7) / 6;
return totalPrice;
}
public int getTotalDate() {
int totalDate = 0;
//add month difference
totalDate += 30 * (endMonth - startMonth);
//add day difference
totalDate += (endDay - startDay);
//add the extra from having 31 days every two months
totalDate += (endMonth - startMonth) / 2;
//if the month start from even months and the end month is different,
//add another day since it ends with 31
//the trick here, if startMonth == endMonth, startMonth/endMonth = 1,
//so 1-1 is 0, nothing get added
//while if startMonth<endMonth, startMonth/endMont = 0, so 1-0 is 1
totalDate += ((startMonth + 1) % 2) * (1 - startMonth / endMonth);
return totalDate;
}
答案 1 :(得分:1)
如果您不介意Java处理循环,则可以使用LocalDate和ChronoUnit。
+------+------+-------+-------+------------+
| Name | Year | Stock | Value | Is_Primary |
+------+------+-------+-------+------------+
| John | 2020 | ABC | 123 | Yes |
| John | 2021 | ABC | 123 | Yes |
| John | 2021 | XYZ | 200 | |
| John | 2022 | ABC | 123 | Yes |
| John | 2022 | XYZ | 200 | |
| John | 2022 | JKL | 500 | |
| John | 2023 | XYZ | 200 | Yes |
| John | 2023 | JKL | 500 | |
+------+------+-------+-------+------------+
答案 2 :(得分:1)
我提出了一些使用查找表的解决方案:
public class PriceCalc {
private static final int FULL_WEEK_PRICE = 18;
private static final int REGULAR_DAYS = 50;
private static final int[] PREV_DAYS = {
0, 31, 61, 92, 122, 153, 183, 214, 244, 275, 305, 336
};
// remainders between start and end dates
private static final int[][] RESTS = {
// S M T W T F S
{ 0, 5, 7, 9, 11, 13, 16},
{13, 0, 2, 4, 6, 8, 10},
{11, 16, 0, 2, 4, 6, 8},
{ 9, 14, 16, 0, 2, 4, 6},
{ 7, 12, 14, 16, 0, 2, 4},
{ 5, 10, 12, 14, 16, 0, 2},
{ 3, 8, 10, 12, 14, 16, 0}
};
private int startDate;
private int startMonth;
private int endDate;
private int endMonth;
public PriceCalc(int startDate, int startMonth, int endDate, int endMonth) {
super();
this.startDate = startDate;
this.startMonth = startMonth;
this.endDate = endDate;
this.endMonth = endMonth;
}
private static int dayOfYear(int day, int month) {
return day + PREV_DAYS[(month - 1) % 12];
}
private static int dayOfWeek(int day, int month) {
return dayOfYear(day, month) % 7;
}
private static int isFlat(int duration) {
int flat = duration / REGULAR_DAYS;
try {
flat /= flat;
return flat;
}
catch(ArithmeticException e) {
return 0;
}
}
private int durationDays() {
int startDOY = dayOfYear(startDate, startMonth);
int endDOY = dayOfYear(endDate, endMonth);
return endDOY - startDOY;
}
public int calcPrice() {
int startDOW = dayOfWeek(startDate, startMonth);
int duration = durationDays();
int flat = isFlat(duration);
int regularDuration = duration * (1 - flat) + REGULAR_DAYS * flat;
int discount = flat * (duration - REGULAR_DAYS);
int fullWeeks = regularDuration / 7;
int rem = regularDuration % 7;
int endDOW = (dayOfYear(startDate, startMonth) + regularDuration) % 7;
int remainder = RESTS[startDOW][endDOW];
int price = fullWeeks * FULL_WEEK_PRICE + remainder + discount;
System.out.printf("Price for %3d days = %2d full weeks + %d days + %3d flat-rate days is: $%3d + $%2d + $%3d = $%3d%n",
duration, fullWeeks, rem, discount,
fullWeeks * FULL_WEEK_PRICE, remainder, discount, price
);
return price;
}
public static void main(String[] args) {
new PriceCalc(1, 1, 4, 1).calcPrice(); // $ 0 + $ 6 + $ 0 = $ 6
new PriceCalc(6, 1, 11, 1).calcPrice(); // $ 0 + $14 + $ 0 = $ 14
new PriceCalc(1, 1, 9, 1).calcPrice(); // $ 18 + $ 2 + $ 0 = $ 20
new PriceCalc(3, 1, 17, 1).calcPrice(); // $ 36 + $ 0 + $ 0 = $ 36
new PriceCalc(4, 1, 23, 1).calcPrice(); // $ 36 + $14 + $ 0 = $ 50
new PriceCalc(27, 1, 5, 3).calcPrice(); // $ 90 + $12 + $ 0 = $102
new PriceCalc(28, 2, 1, 6).calcPrice(); // $126 + $ 2 + $ 45 = $173
}
}
答案 3 :(得分:0)
也许创建者希望您找到公式。我有根据和您的代码:
public int getPrice() {
getTotalDate();
int comboDate;
int normalDate;
if (totalDate > 50){
comboDate = totalDate - 50;
normalDate = 50;
} else {
comboDate = 0;
normalDate = totalDate;
}
int sunDay = normalDate/7;
int satDay = normalDate/7 + (normalDate%6)/6;
int weekDay = normalDate - satDay - sunDay;
return comboDate*1 + sunDay*5 + satDay*3 + weekDay*2;
}
public int getTotalDate() {
int start = startMonth*30 + (startMonth-1)/2 + startDay;
int end = endMonth*30 + (endMonth-1)/2 + endDay;
totalDate = end - start + 1;
return totalDate;
}