前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDOJ(HDU) 2133 What day is it(认识下Java的Calendar类---日期类)

HDOJ(HDU) 2133 What day is it(认识下Java的Calendar类---日期类)

作者头像
谙忆
发布2021-01-21 15:40:38
2080
发布2021-01-21 15:40:38
举报
文章被收录于专栏:程序编程之旅程序编程之旅

Problem Description Today is Saturday, 17th Nov,2007. Now, if i tell you a date, can you tell me what day it is ?

Input There are multiply cases. One line is one case. There are three integers, year(0< year<10000), month(0<=month<13), day(0<=day<32).

Output Output one line. if the date is illegal, you should output “illegal”. Or, you should output what day it is.

Sample Input 2007 11 17

Sample Output Saturday

这个题目的某个日期是星期几,和真正的日历是不一样的!!! 所以,用Java的日期类Calendar是过不了的。 只能自己写囖。。。题目意思是:1 1 1是星期一! 而事实上,1 1 1是星期六。不要问我为什么,我也不知道。。。

代码语言:javascript
复制
package cn.hncu.acm;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

public class P2133 {


    public static void main(String[] args) throws ParseException {
        String[] week = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
        int yuee[][]={{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}};
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int y = sc.nextInt();
            int m = sc.nextInt();
            int d = sc.nextInt();
             if((yunn(y)==0&&m==2&&d==29)||m>12||d>yuee[yunn(y)][m]||m==0||d==0||y==0)
               {
                   System.out.println("illegal");
                   continue;
               }
             /*
            //题目是有问题的
            //1 1 1 应该是星期六,具体为什么看网上资料。
            //这个题目要求已知1 1 1是星期一
            String[] week = {"","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
            String strTime = y+"-"+m+"-"+d;
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(format.parse(strTime));
            System.out.println(week[c.get(Calendar.DAY_OF_WEEK)]);

            */
             int s=0;
             for(int i=1;i<y;i++){
                 if(yunn(i)==1){
                     s+=366;
                 }else{
                     s+=365;
                 }
             }

             for(int i=1;i<m;i++){
                 s+=yuee[yunn(y)][i];
             }
             s+=d;
             s=s%7;
             System.out.println(week[s]);
        }
    }


    public static int yunn(int xx)
    {
        if((xx%4==0&&xx%100!=0)||(xx%400==0))
            return 1;//是闰年
        return 0;//不是闰年
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016/05/03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档