NowCoder在淘宝上开了一家网店。他发现在月份为素数的时候,当月每天能赚1元;否则每天能赚2元。
现在给你一段时间区间,请你帮他计算总收益有多少。
输入包含多组数据。
每组数据包含两个日期from和to (2000-01-01 ≤ from ≤ to ≤ 2999-12-31)。
日期用三个正整数表示,用空格隔开:year month day。
对应每一组数据,输出在给定的日期范围(包含开始和结束日期)内能赚多少钱。
2000 1 1 2000 1 31 2000 2 1 2000 2 29
62 29
import java.util.Scanner;
import java.time.LocalDate;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int year = in.nextInt();
int month = in.nextInt();
int day = in.nextInt();
int year1 = in.nextInt();
int month1 = in.nextInt();
int day1 = in.nextInt();
LocalDate L1 = LocalDate.of(year, month, day);
LocalDate L2 = LocalDate.of(year1, month1, day1);
System.out.println(date(L1, L2));
}
}
//计算获取收益
public static int date(LocalDate L1, LocalDate L2) {
int count = 0;
while (!L1.isAfter(L2)) {
if (isSuShu(L1.getMonthValue())) {
count++;
}else{
count+=2;
}
L1 = L1.plusDays(1);
}
return count;
}
private static boolean isSuShu(int month) {
if (month == 2 || month == 3 || month == 5 || month == 7 || month == 11) {
return true;
} else {
return false;
}
}
}
大佬帮忙看看我这错哪里呢?
在IDEA上没报错 但是在牛客上出现这个错误,于是我便把他的测试用例每个单独运行,结果都对着
这为啥?
上面报错 是2100这年2月没有29天,但是我测试是发现结果为28天对着,这是为啥,
有没有大佬帮忙分析分析。万分感谢! import java.util.*;
/*
* 淘宝网店
*/
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNextInt()) {
int fromYear=sc.nextInt();
int fromMonth=sc.nextInt();
int fromDay=sc.nextInt();
int toYear=sc.nextInt();
int toMonth=sc.nextInt();
int toDay=sc.nextInt();
Set<Integer>set=new HashSet<>();//添加素数方便查找
set.add(2);set.add(3);set.add(5);
set.add(7);set.add(11);
int money=0;
int day=0;
if(fromYear==toYear) {
if(fromMonth==toMonth) {
money=sameMonth(fromDay, fromMonth, fromYear, toDay, toMonth, toYear, set);
}else {
money=sameYear(fromDay, fromMonth, fromYear, toDay, toMonth, toYear, set);
}
}else {//不同年
money+=sameYear(fromDay, fromMonth, fromYear, 31, 12, fromYear, set);//第一年
money+=sameYear(1, 1, toYear, toDay, toMonth, toYear, set);//最后一年
int temp=fromYear+1;
while( temp<toYear){
money+=sameYear(1, 1, temp, 31, 12, temp, set);
temp++;
}
}
System.out.println(money);
}
}
//是否是闰年
public static boolean isLeapYear(int year) {
return (year%4==0&&year%100!=0)||year%400==0;
}
//计算某个月多少天
public static int manyDay(int year,int month) {
switch(month) {
case 1:case 3:case 5 :
case 7:case 8: case 10:case 12:return 31;
case 4:case 6:case 9:case 11:return 30;
default:
if(isLeapYear(year)==true) {
return 29;
}
else {
return 28;
}
}
}
//同年不同月
public static int sameYear(int fromDay,int fromMonth,int fromYear,int toDay,int toMonth,int toYear,Set<Integer>set) {
int money=0;
int day=0;
day=manyDay(fromYear, fromMonth)-fromDay+1;//初始月的天数,包括初始日期
if(set.contains(fromMonth)) {//是否是素月
money+=day*1;
}else {
money+=day*2;
}
day=toDay;//结束月的天数
if(set.contains(toMonth)) {
money+=day*1;
}else {
money+=day*2;
}
//中间月
for(int i=fromMonth+1;i<toMonth;i++) {
day=manyDay(fromYear, i);
if(set.contains(i)) {
money+=day*1;
}else {
money+=day*2;
}
}
return money;
}
//同年同月
public static int sameMonth(int fromDay,int fromMonth,int fromYear,int toDay,int toMonth,int toYear,Set<Integer>set) {
int money=0;
int day=0;
day=toDay-fromDay+1;//初始月的天数,包括初始日期
if(set.contains(fromMonth)) {//是否是素月
money+=day*1;
}else {
money+=day*2;
}
return money;
}
} import java.util.Scanner;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {// 注意,如果输入是多个测试用例,请通过while循环处理多个测试用例
LocalDate start = LocalDate.of(in.nextInt(),in.nextInt(),in.nextInt());
LocalDate end = LocalDate.of(in.nextInt(),in.nextInt(),in.nextInt());
int money = 0;
while(start.compareTo(end)<=0){
int month = start.getMonthValue();
if(month==2||month==3||month==5||month==7||month==11){
money+=1;
}else{
money+=2;
}
start=start.plusDays(1);
}
System.out.println(money);
}
}
} import java.util.*;
public class Main{
//判断是否是闰年
public static boolean isLeapYear(int n){
if((n % 4 == 0 && n % 100 != 0) || n % 400 == 0){
return true;
}
return false;
}
//判断某一年一共有多少收益
public static int profitofYear(int year){
return 2 * 31
+ 1 * 28
+ 1 * 31
+ 2 * 30
+ 1 * 31
+ 2 * 30
+ 1 * 31
+ 2 * 31
+ 2 * 30
+ 2 * 31
+ 1 * 30
+ 2 * 31
+ (isLeapYear(year) ? 1 : 0);
}
//判断某一月是否是素数
public static boolean isPrime(int month){
return (month == 2 || month == 3 || month == 5 || month== 7 || month == 11);
}
//计算从year年的1月1号到year年的month月day号的收益
public static int profitodThisyear(int year,int month,int day){
int profit = 0;
if(!isPrime(month)){
profit = day * 2;
}else{
profit = day;
}
while (--month > 0) {
switch (month) {
case 1: case 8: case 10: case 12 :
profit += 62;
break;
case 3: case 5: case 7:
profit += 31;
break;
case 4 : case 6 : case 9:
profit += 60;
break;
case 11:
profit += 30;break;
default:
profit += (28 + (isLeapYear(year) ? 1 : 0));
break;
}
}
return profit;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int profit = 0;
int year1 = sc.nextInt();
int month1 = sc.nextInt();
int day1 = sc.nextInt();
int year2 = sc.nextInt();
int month2 = sc.nextInt();
int day2 = sc.nextInt();
profit = profitofYear(year1) - profitodThisyear(year1,month1,day1 - 1);
profit += profitodThisyear(year2,month2,day2);
if(year1 == year2){
profit -= profitofYear(year1);
}
for(int i = year1 + 1;i < year2;i++){
profit += profitofYear(i);
}
System.out.println(profit);
}
}
} // write your code here
import java.util.*;
public class Main{
//判断是否是闰年
public static boolean isLeapYear(int year){
return (year%4==0&&year%100!=0)||year%400==0;
}
//一整年的收益
public static int profitYear(int year){
return 2*31
+1*28
+1*31
+2*30
+1*31
+2*30
+1*31
+2*31
+2*30
+2*31
+1*30
+2*31
+(isLeapYear(year)?1:0);
}
//判断月份是否是素数
public static boolean isPrime(int month){
return month==2||month==3||month==5||month==7||month==11;
}
//本年具体日期的收益
public static int profitThisyear(int year,int month,int day){
int profit=0;
if(isPrime(month)){
profit=day;
}else{
profit=2*day;
}
while(--month>0){
switch(month){
case 1:case 8:case 10:case 12:
profit+=62;
break;
case 3:case 5:case 7:
profit+=31;
break;
case 4:case 6:case 9:
profit+=60;
break;
case 11:
profit+=30;
break;
default:
profit+=(28+(isLeapYear(year)?1:0));
break;
}
}
return profit;
}
public static void main(String[]args){
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext()){
int profit=0;
int startYear=scanner.nextInt();
int startMonth=scanner.nextInt();
int startDay=scanner.nextInt();
int endYear=scanner.nextInt();
int endMonth=scanner.nextInt();
int endDay=scanner.nextInt();
profit=profitYear(startYear)-profitThisyear(startYear,startMonth,startDay-1);
profit+=profitThisyear(endYear,endMonth,endDay);
if(startYear==endYear){//年份相同的情况
profit-=profitYear(startYear);
}
for(int i=startYear+1;i<endYear;i++){
profit+=profitYear(i);
}
System.out.println(profit);
}
}
} import java.util.*;
public class Main{
// 平年一年每个月份的收入
static int[] moneyOfMonth = {62, 28, 31, 60, 31, 60, 31, 62, 60, 62, 30, 62};
// 判断闰年
public static boolean isLeap(int year){
if(year % 400 == 0 || year % 4 == 0 && year % 100 != 0){
return true;
}
return false;
}
// 判断素数月
public static boolean isPrime(int month){
if(month == 2 || month == 3 || month == 5 || month == 7 || month == 11) return true;
return false;
}
// 计算完整一年的收入
public static int moneyOfYear(int year){
int ret = 0;
for(int i = 0; i < moneyOfMonth.length; i++){
ret += moneyOfMonth[i];
}
if(isLeap(year)){
return ret+1;
}
return ret;
}
// 计算同年收入
public static int moneyOfIncompleteYear(int[] from, int[] to){
// 同月
if(from[1] == to[1]){
if(isPrime(from[1])){
return to[2]-from[2]+1;
}
else{
return 2*(to[2]-from[2]+1);
}
}
int ret = 0;
// 1.计算from月收入
if(isPrime(from[1])){
ret += moneyOfMonth[from[1]-1] - (from[2]-1);
}
else{
ret += moneyOfMonth[from[1]-1] - 2*(from[2]-1);
}
if(from[1] == 2 && isLeap(from[0])) ret++;
// 2.计算从from当月的下一月到to的前一月这几月完整的月收入
for(int i = from[1]+1-1; i < to[1]-1; i++){
ret += moneyOfMonth[i];
if(i == 2 && isLeap(from[0])) ret++;
}
// 3.计算to月收入
if(isPrime(to[1])){
ret += to[2];
}
else{
ret += 2*to[2];
}
return ret;
}
// 计算从from到to的收入
public static int money(int[] from, int[] to){
// 同年
if(from[0] == to[0]){
return moneyOfIncompleteYear(from, to);
}
int ret = 0;
// 1.计算from当年的收入
if(isPrime(from[1])){
ret += moneyOfMonth[from[1]-1] - from[2]-1;
}
else{
ret += moneyOfMonth[from[1]-1] - 2*(from[2]-1);
}
if(from[1] == 2 && isLeap(from[0])) ret++;
for(int i = from[1]+1-1; i < 12; i++){//+1是因为要计算的后一个月的 -1是因为数组下标
ret += moneyOfMonth[i];
if(i == 1 && isLeap(from[0])) ret++;
}
// 2.计算从from当年的下一年到to的前一年这几年完整的年收入
for(int i = from[0]+1; i < to[0]; i++){
ret += moneyOfYear(i);
}
// 3.计算to当年收入
for(int i = 0; i < to[1]-1; i++){
ret += moneyOfMonth[i];
if(i == 1 && isLeap(to[0])) ret++;
}
if(isPrime(to[1])){
ret += to[2];
}
else{
ret += 2*to[2];
}
return ret;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int[] from = new int[3];
for(int i = 0; i < 3; i++){
from[i] = sc.nextInt();
}
int[] to = new int[3];
for(int i = 0; i < 3; i++){
to[i] = sc.nextInt();
}
int ret = money(from, to);
System.out.println(ret);
}
}
}