方法一:硬算
import java.util.Scanner;
public class Main {
static int y[] = new int[2051];
static int m1[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for(int i = 1850; i <= 2050; i++) {
if(i%400==0||(i%4==0&&i%100!=0)) {
y[i] = 1;
}
}
int m = scanner.nextInt();
int n = scanner.nextInt();
int w = scanner.nextInt();
int s = scanner.nextInt();
int e = scanner.nextInt();
for(int i = s;i<=e;i++) {
int day = 0;
for(int j =1850;j<i;j++) {
day = day+365+y[j];
}
for(int j = 1; j < m;j++) {
day+=m1[j];
if(j==2) day+=y[i];
}
int ww = day%7+2;
if(ww>7)ww = ww-7;
int cnt = 0,flag = 0;
int mm = (m==2?y[i]:0)+m1[m];
for(int k = 1; k <= mm;k++) {
if(w==ww)cnt++;
if(cnt==n) {
flag =k;
break;
}
ww = ww+1==8?1:ww+1;
}
if(flag == 0) System.out.println("none");
else System.out.println(String.format("%d/%02d/%02d", i,m,flag));
}
}
}
方法二:JavaAPI
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
public class Main {
static int mm[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
int w = scanner.nextInt();
int s = scanner.nextInt();
int e = scanner.nextInt();
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy/MM/dd");
for(int i = s; i <= e; i++) {
String q = String.format("%d/%02d/%02d", i,m,1);
LocalDate ldt = LocalDate.parse(q, f);
int ww = ldt.getDayOfWeek().getValue();
mm[2] = ldt.isLeapYear()?29:28;
int cnt = 0;
int k = 1+(w+7-ww);
if(k>7) k-=7;
while(k<=mm[m]) {
cnt++;
if(cnt==n) {
System.out.println(String.format("%d/%02d/%02d", i,m,k));
break;
}
k+=7;
}
if(k>mm[m])System.out.println("none");
}
}
}