文章目录
1、引言C++版本:Java版本:python版:1、引言
泽勒一致性是由克里斯汀-泽勒开发的用于计算某天是星期几的计算公式。计算公式如下:
h=(q+[26*(m+1)/10]+k+[k/4]+[j/4]+5*j)%7其中:h是一个星期中每一天(0是星期六,1是星期天,2是星期一,3是星期二,4是星期三,5是星期四,6是星期五)q是某月的天数m是月份(3为三月,4为四月,……,12为十二月。其中一月和二月分别为上一年的13月和14月。)j是世纪数,即year/100k是世纪的年数,即year%100所求的月份如果是1月或2月,则应视为前一年的13月或14月. 所以公式中m 的取值范围不是1-12,而是3-14.
C++版本:
#include<bits/stdc++.h>using namespace std;int main(){int year,mon,day;while(~scanf("%d/%d/%d",&year,&mon,&day)){if(mon==1){mon=13;year-=1;}else if(mon==2){mon=14;year-=1;}int century=year/100;int yearOfCentury=year%100;int theDayIs = (day + 26 * (mon + 1) / 10 + yearOfCentury + yearOfCentury / 4 + century / 4 + 5 * century) % 7;switch(theDayIs){case 0: cout<<"星期六"<<endl; break;case 1: cout<<"星期天"<<endl; break;case 2: cout<<"星期一"<<endl; break;case 3: cout<<"星期二"<<endl; break;case 4: cout<<"星期三"<<endl; break;case 5: cout<<"星期四"<<endl; break;case 6: cout<<"星期五"<<endl; break;default: cout<<"计算错误!"<<endl;}}return 0;}
我们来测试一下今天是星期几
输入/1/15
理想结果:星期三
实际结果:
没问题~
Java版本:
/** 运用泽勒一致性计算某天是星期几?* 公式:*h = (q + 26(m+1)/10 + k +k/4 + j/4 +5j) % 7* 其中:** h是一个星期中每一天(0是星期六,1是星期天,2是星期一,3是星期二,4是星期三,5是星期四,6是星期五)* q是某月的天数* m是月份(3为三月,4为四月,……,12为十二月。其中一月和二月分别为上一年的13月和14月。)* j是世纪数,即year/100* k是世纪的年数,即year%100**/import java.util.Scanner;public class Zeller {public static void main(String args[]){Scanner imput = new Scanner(System.in);System.out.print("Enter a year: (e.g, ): ");int year = imput.nextInt();System.out.print("Enter a mouth: 1-12: ");int mouth = imput.nextInt();System.out.print("Enter the day of the mouth: ");int day = imput.nextInt();int tempMouth = mouth;//一月二月记为13,14,年数记为上一年switch (tempMouth){case 1: mouth = 13; year -=1; break;case 2: mouth = 14; year -=1; break;}int century = year / 100;int yearOfCentury = year % 100;int theDayIs = (day + 26 * (mouth + 1) / 10 + yearOfCentury + yearOfCentury / 4 + century / 4 + 5 * century) % 7;//0为周六1为周日2为周一3为周二4为周三5为周四6为周五switch (theDayIs){case 0 :System.out.println("Day of the week is Saturday"); break;case 1 :System.out.println("Day of the week is Sunday"); break;case 2 :System.out.println("Day of the week is Monday"); break;case 3 :System.out.println("Day of the week is Tuesday"); break;case 4 :System.out.println("Day of the week is Wednesday"); break;case 5 :System.out.println("Day of the week is Thursday"); break;case 6 :System.out.println("Day of the week is Friday"); break;}}}
python版:
def calculateDayofWeek(q,m,k,j):h = (q + (26*(m + 1) // 10) + k + (k // 4) + (j // 4) + 5*j) % 7#泽勒一致性return hdef info_enter():year = eval(input("请输入年份:"))j = year // 100#世纪数month = eval(input("请输入月份:"))if month == 1:m = 13k = year % 100 - 1#年份elif month == 2:m = 14k = year % 100 - 1#年份else:m = monthk = year % 100#年份day = eval(input("请输入月份中哪一天:"))q = dayreturn q, m, k, j,day,month,yearif __name__ == "__main__":q,m,k,j,day,month,year = info_enter()#求出日期对应着一个星期的哪一天h = calculateDayofWeek(q, m, k, j)if h == 0:week = 'Saturday'elif h == 1:week = 'Sunday'elif h == 2:week = 'Monday'elif h == 3:week = 'Tuesday'elif h == 4:week = 'Wednesday'elif h == 5:week = 'Thursday'elif h == 6:week = 'Friday'print(str(year)+'-'+str(month)+'-'+str(day)+" is "+week+' of the week.')
学如逆水行舟,不进则退
算法:泽勒的一致性 给定一个日期 输出这个日期是该年一周中的星期几 【c++ java python版本】