题目
( 科学:某天是星期几)泽勒一致性是由克里斯汀•泽勒开发的用于计算某天是星期几
的算法。这个公式是:
其中:
h 是一个星期中的某一天(0 为星期六; 1 为星期天; 2 为星期一; 3 为星期二; 4 为
星期三; 5 为星期四; 6 为星期五)。
q 是某月的第几天。
m 是月份( 3 为三月, 4 为四月, ……, 12 为十二月)。一月和二月分别记为上一年
的 13 和 14 月。
j 是世纪数(即 )。
k 是该世纪的第几年( year%100)。
注意,公式中的除法执行一个整数相除。编写程序,提示用户输入年、月和该月的哪一
天,然后显示它是一周中的星期几。下面是一些运行示例:
Enter year:( e.g., 2012) :2015
Enter month: 1-12: 1
Enter the day of the month: 1-31: 25
Day of the week is Sunday
Enter year:( e.g., 2012) : 2012
Enter month: 1-12: 5
Enter the day of the month: 1-31: 12
Day of the week is Saturday
提示:一月和二月在这个公式里是用 13 和 14 表示的。所以需要将用户输入的月份 1 转
换为 13, 将用户输入的月份 2 转换为 14, 同时将年份改为前一年。
class Tang_01_17
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.print("Enter year:e.(e.g, 2012) : ");
int year=input.nextInt();
System.out.print("Enter month: 1-12 : ");//m
int m=input.nextInt();
if(m==1)
{
m=13;
year=year-1;
}
if(m==2)
{
m=14;
year=year-1;
}
System.out.print("Enter the day of the month: 1-31 : ");//q
int q=input.nextInt();
int k=year%100;
int j=Math.abs(year/100);
int h=(q+26*(m+1)/10+k+k/4+j/4+5*j)%7;
switch(h)
{
case 0:
System.out.print("Day of the week is Saturday ");
break;
case 1:
System.out.print("Day of the week is Sunday ");
break;
case 2:
System.out.print("Day of the week is Monday");
break;
case 3:
System.out.print("Day of the week is Tuesday ");
break;
case 4:
System.out.print("Day of the week is Wednesday ");
break;
case 5:
System.out.print("Day of the week is Thursday ");
break;
case 6:
System.out.print("Day of the week is Friday ");
break;
}
}
}