代码如下:
import java.util.Scanner; public class Main { static int a[][] = { { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; public static void main(String[] args) { Scanner input = new Scanner(System.in); int y = input.nextInt(); int m = input.nextInt(); int d = input.nextInt(); int week = Day(y, m, d) - Day(2011, 11, 11); if (week > 0) System.out.println((4 + week % 7) % 7 + 1); else { for (int i = 1; i <= 7; ++i) if ((i - 1 + -week % 7) % 7 + 1 == 5) { System.out.println(i); break; } } } public static boolean isRun(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return true; return false; } public static int Day(int y, int m, int d) { int ans = 0; for (int i = 1; i < y; ++i) ans += calDay(i); int index = 0; if (!isRun(y)) index = 1; for (int i = 1; i < m; ++i) ans += a[index][i]; ans += d; return ans; } public static int calDay(int year) { int index = 0; if (!isRun(year)) index = 1; int tot = 0; for (int i = 1; i <= 12; ++i) tot += a[index][i]; return tot; } }运行结果:
2011 11 11
5