牛客网连接:https://www.nowcoder.com/questionTerminal/754921e9c98b43d1b2d70c227b844101?toCommentId=13055397 NowCoder在淘宝上开了一家网店。他发现在月份为素数的时候,当月每天能赚1元;否则每天能赚2元。 现在给你一段时间区间,请你帮他计算总收益有多少。
案例已全部通过
import java.util.*;
public class Main{
public static int month(int year,int month) {
if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 29;
} else {
return 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
} else {
return 31;
}
}
public static int func(int m) {
int x=0;
if (m == 2 || m == 3 || m == 5 || m == 7 || m == 11) {
x = 1;
} else {
x = 2;
}
return x;
}
public static void main (String[]args){
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int y1 = scanner.nextInt();
int m1 = scanner.nextInt();
int d1 = scanner.nextInt();
int y2 = scanner.nextInt();
int m2 = scanner.nextInt();
int d2 = scanner.nextInt();
int sum = 0;
if (y1 - y2 == 0) {
for (int m = m1; m <= m2; m++) {
int x = func(m);
if (m1 != m2) {
if (m == m1) {
sum += (month(y1, m1) - d1 + 1) * x;
} else if (m > m1 && m < m2) {
sum += month(y1, m) * x;
} else {
sum += d2 * x;
}
} else {
sum += (d2 - d1 + 1) * x;
}
}
} else {
for (int m = m1; m <= 12; m++) {
int x = func(m);
if (m == m1) {
sum += (month(y1, m1) - d1 + 1) * x;
} else {
sum += month(y1, m) * x;
}
}
if (y2 - y1 > 1) {
int tmpYear = y1 + 1;
while (tmpYear < y2) {
for (int m = 1; m <= 12; m++) {
int x = func(m);
sum += month(tmpYear, m)*x;
}
tmpYear++;
}
}
for (int m = 1; m < m2; m++) {
int x = func(m);
sum += month(y2, m) * x;
}
int x = func(m2);
sum += d2*x;
}
System.out.println(sum);
}
}
}
|