| 蔡勒演算法 |
其中w代表星期
c代表世紀數減1(年份前兩位數)
y代表年(兩位數)
m代表月(m大於等於3,小於等於14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月來計算,比如2003年1月1日要看作2002年的13月1日來計算)
d代表日
[ ]稱作高斯符號,代表取整,即只要整數部份。
mod代表同餘(這裡代表括號裡除以7後的餘數)
//注:資料取自維基百科
import java.util.Scanner;
public class C1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//創建並提示使用者輸入年,月,日
System.out.println("Enter year: (e.g. , 2012)");
int a = input.nextInt(); //年
System.out.println("Enter month: 1-12:");
int m = input.nextInt(); //月
System.out.println("Enter the day of month: 1-31:");
int c= input.nextInt(); //日
int q = c;
//判斷是否為1月或2月,如果是,年數要減1
if(m == 1 || m == 2)
{
a = a - 1;
}
//計算世紀與年份
int j = a / 100; //世紀
int k = a % 100; //年份
//計算m,1月為13月、2月為14月,其餘月份不變
if( m == 1 )
{
m = 13;
}
if( m == 2)
{
m = 14;
}
if( 3 >= m || m <= 12 )
{
m = m;
}
//計算
int d = 26 * ( m + 1 ) / 10;
//計算剩公式剩餘部分
int h = ( q + d + k + ( k /4 ) + ( j / 4 ) + ( 5 * j ) )% 7;
//判斷該日為星期幾
if(h == 0)
{
System.out.println("Day of the week is Saturday");
}
if(h == 1)
{
System.out.println("Day of the week is Sunday");
}
if(h == 2)
{
System.out.println("Day of the week is Monday");
}
if(h == 3)
{
System.out.println("Day of the week is Tuesday");
}
if(h == 4)
{
System.out.println("Day of the week is Wednesday");
}
if(h == 5)
{
System.out.println("Day of the week is Thursday");
}
if(h == 6)
{
System.out.println("Day of the week is Friday");
}
}
}
沒有留言:
張貼留言