62,621
社区成员
发帖
与我相关
我的任务
分享
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
/**
* Hdu 2133 What day is it
* <p>题目:<a href=http://acm.hdu.edu.cn/showproblem.php?pid=2133>
* http://acm.hdu.edu.cn/showproblem.php?pid=2133</a></p>
* @author ps
*/
public class Hdu2133 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
while((str = br.readLine()) != null){
String []list = str.split(" ");
int year = Integer.parseInt(list[0]);
int month = Integer.parseInt(list[1]);
int day = Integer.parseInt(list[2]);
if(year<=0 || year>=10000 ||month<=0
||month>=13 ||day<=0 ||day>=32){
System.out.println("illegal");
continue;
}
//处理有些月没有31号的情况
if(4==month||6==month||9==month||11==month){
if(day>30){
System.out.println("illegal");
continue;
}
}
//处理某些月有多余日如:2月在闰年时没有30/31在平年时没有29/30/31
if(2==month){
boolean isLeapYear = isLeapYear(year);
if(isLeapYear && day>29){
System.out.println("illegal");
continue;
}else if(!isLeapYear && day>28){
System.out.println("illegal");
continue;
}
}
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
cal.set(year, month-1, day);
int date = cal.get(Calendar.DAY_OF_WEEK);
switch(date){
case Calendar.MONDAY:
System.out.println("Monday");
break;
case Calendar.TUESDAY:
System.out.println("Tuesday");
break;
case Calendar.WEDNESDAY:
System.out.println("Wednesday");
break;
case Calendar.THURSDAY:
System.out.println("Thursday");
break;
case Calendar.FRIDAY:
System.out.println("Friday");
break;
case Calendar.SATURDAY:
System.out.println("Saturday");
break;
case Calendar.SUNDAY:
System.out.println("Sunday");
break;
}
}
}
/**
* 判断是否是闰年
* @param year 年
* @return 是返回true,否返回false
*/
public static boolean isLeapYear(int year){
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
return true;
}
return false;
}
}
package com.nt;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Hdu 2133 What day is it
* <p>
* 题目:<a href=http://acm.hdu.edu.cn/showproblem.php?pid=2133>
* http://acm.hdu.edu.cn/showproblem.php?pid=2133</a>
* </p>
* <p>
* 计算某一天是星期几的计算公式:
* </p>
* <p>
* 一:常用公式W = [Y-1] + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D
* Y是年份数,D是这一天在这一年中的累积天数,也就是这一天在这一年中是第几天。
* </p>
* <p>
* 二:蔡勒(Zeller)公式w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
* 公式中的符号含义如下,w:星期;c:世纪;y:年(两位数); m:月(m大于等于3,小于等于14,即在蔡勒公式中,
* 某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算);d:日;[ ]代表取整,即只要整数部分。
* 相比于通用通用计算公式而言,蔡勒(Zeller)公式大大降低了计算的复杂度。
* </p>
* <p>
* 三:对蔡勒(Zeller)公式的改进.作者:冯思琮
* 相比于另外一个通用通用计算公式而言,蔡勒(Zeller)公式大大降低了计算的复杂度。不过,笔者给出的通用计算公式似乎更加简洁(包括运算过程)。现将公式列于其下:
* W=[y/4]+r (y/7)-2r(c/4)+m’+d 公式中的符号含义如下,r (
* )代表取余,即只要余数部分;m’是m的修正数,现给出1至12月的修正数1’至12’如下:
* (1’,10’)=6;(2’,3’,11’)=2;(4’,7’)=5;5’=0;6’=3;8’=1;(9’,12’)=4
* (注意:在笔者给出的公式中,y为润年时1’=5;2’=1)。其他符号与蔡勒(Zeller)公式中的含义相同。
* </p>
* <p>
* 四:基姆拉尔森计算公式,这个公式名称是我给命名的,哈哈希望大家不要见怪。 W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)
* mod 7 在公式中d表示日期中的日数,m表示月份数,y表示年数。 注意:在公式中有个与其他公式不同的地方:
* 把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
* </p>
*
* @author ps
*/
public class Hdu2133 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
while((str = br.readLine()) != null){
String []list = str.split(" ");
int year = Integer.parseInt(list[0]);
int month = Integer.parseInt(list[1]);
int day = Integer.parseInt(list[2]);
if(year<=0 || year>=10000 ||month<=0
||month>=13 ||day<=0 ||day>=32){
System.out.println("illegal");
continue;
}
//处理有些月没有31号的情况
if(4==month||6==month||9==month||11==month){
if(day>30){
System.out.println("illegal");
continue;
}
}
//处理某些月有多余日如:2月在闰年时没有30/31在平年时没有29/30/31
if(2==month){
boolean isLeapYear = isLeapYear(year);
if(isLeapYear && day>29){
System.out.println("illegal");
continue;
}else if(!isLeapYear && day>28){
System.out.println("illegal");
continue;
}
}
//由于使用calendar会导致计算出来的日期可能不正确,
//所以我采用了蔡勒(Zeller)公式w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
if(1==month || 2==month){
month+=12;
year--;
}
int cent = year/100;//表示世纪
year = year%100;//年为两位数。
int week = year+(year/4)+(cent/4)-2*cent+(26*(month+1))/10+day-1;
while(week<0){
week += 7;
}
week %= 7;
switch(week){
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 0:
System.out.println("Sunday");
break;
}
}
}
/**
* 判断是否是闰年
* @param year 年
* @return 是返回true,否返回false
*/
public static boolean isLeapYear(int year){
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
return true;
}
return false;
}
}
import java.util.Calendar;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
System.out.println("Please input a string of date(formatted as \"yyyyMMdd\").\n" +
"If you want to exit, input an empty string.");
Scanner scanner = new Scanner(System.in);
String s;
while (scanner.hasNextLine()) {
s = scanner.nextLine().trim();
if (s.isEmpty())
break;
getDay(s);
System.out.println("Please input another string of date.");
}
System.out.println("Program exited.");
}
public static void getDay(String s) {
try {
String[] arr = s.trim().split(" +");
if (arr.length != 3) {
System.out.println("Invalid input.");
return;
}
Calendar calendar = Calendar.getInstance();
calendar.setLenient(false);
calendar.set(new Integer(arr[0]), new Integer(arr[1]) - 1, new Integer(arr[2]));
int day = calendar.get(Calendar.DAY_OF_WEEK);
switch (day) {
case Calendar.SUNDAY:
System.out.println("Sunday.");
break;
case Calendar.MONDAY:
System.out.println("Monday.");
break;
case Calendar.TUESDAY:
System.out.println("Tuesday.");
break;
case Calendar.WEDNESDAY:
System.out.println("Wednesday.");
break;
case Calendar.THURSDAY:
System.out.println("Thursday.");
break;
case Calendar.FRIDAY:
System.out.println("Friday.");
break;
case Calendar.SATURDAY:
System.out.println("Saturday.");
break;
default:
System.out.println("Invalid input.");
return;
}
} catch (Exception e) {
System.out.println("Invalid input.");
}
}
}