There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.
Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case. Month and Week name in Input/Output: January, February, March, April, May, June, July, August, September, October, November, December Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
9 October 2001 14 October 2001
Tuesday Sunday
import java.util.*; public class Main{ static int months[][]= { {0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31} }; static int isLeapYear(int year){ if(year%4==0&&year%100!=0||year%400==0){ return 1; }return 0; } static int getMonth(String m){ Map<String,Integer>map=new HashMap<>(); map.put("January", 1); map.put("Febrary", 2); map.put("March", 3); map.put("April", 4); map.put("May", 5); map.put("June", 6); map.put("July", 7); map.put("August", 8); map.put("September", 9); map.put("October", 10); map.put("November", 11); map.put("December", 12); int month=map.get(m); return month; } static String getWeek(int n){ Map<Integer,String>map=new HashMap<>(); map.put(0, "Sunday"); map.put(1, "Monday"); map.put(2, "Tuesday"); map.put(3, "Wednesday"); map.put(4, "Thursday"); map.put(5, "Friday"); map.put(6, "Saturday"); String week=map.get(n); return week; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { //输入 int day=sc.nextInt(); String m=sc.next(); int month=getMonth(m); int year=sc.nextInt(); int n=help(year,month,day); System.out.println(getWeek(n%7)); } } private static int help(int y, int m, int d) { int day=1; int y1=1,m1=1,d1=1; while(y1<y||m1<m||d1<d) { d1++; if(d1==months[isLeapYear(y1)][m1]+1){ d1=1; m1++; } if(m1==13){ m1=1; y1++; } day++; } return day; } }卡在了求日期间隔的地方一个多小时,不知道为什么,下面求日期的做法就是错的。。
//很好理解啊,为啥错了呢??? int help(int y, int m, int d) { int sum=0; for(int i=1;i<y;i++) { sum+=365; sum+=isLeapYear(y); } for(int i=1;i<m;i++) { sum+=months[isLeapYear(y)][i]; } return sum+=d; }
import java.time.LocalDate; import java.time.Month; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ int day = scanner.nextInt(); String month = scanner.next(); int year = scanner.nextInt(); LocalDate date = LocalDate.of(year, Month.valueOf(month.toUpperCase()), day); String s = date.getDayOfWeek().toString().toLowerCase(); System.out.println(s.substring(0,1).toUpperCase()+s.substring(1)); } } }