首页 > 试题广场 >

Day of Week

[编程题]Day of Week
  • 热度指数:23188 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400. For example, years 2004, 2180 and 2400 are leap. Years 2005, 2181 and 2300 are not leap. Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

输入描述:
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
示例1

输入

9 October 2001
14 October 2001

输出

Tuesday
Sunday
java版本
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;
}



发表于 2020-04-11 09:43:26 回复(0)

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Main {
    private static String month[] = {
            "","January", "February","March", "April",
            "May", "June", "July", "August", "September",
            "October", "November", "December"
    };
    private static String week[] = {
            "Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
    };
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        String date = sc.nextLine();
        String[] dates = date.split(" ");
        int m = 1;
        for (int i=1;i<month.length;i++){
            if (dates[1].equals(month[i])){
                m = i;
            }
        }
        String M = m < 10 ? "0" + m : "" + m;
        String day = dates[2] + M + dates[0];
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Date fd = format.parse(day);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(fd);
        int dw = calendar.get(calendar.DAY_OF_WEEK);
        System.out.println(week[dw - 1]);
    }
}

发表于 2020-03-21 11:22:15 回复(0)
Java

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));
        }
    }
}


发表于 2020-03-19 23:34:54 回复(1)

问题信息

难度:
3条回答 11944浏览

热门推荐

通过挑战的用户

查看代码
Day of Week