回复 9楼 beyondyf
就楼主的题目来说 不使用公式算 就得选个基准日期 比如1998年1月1日是星期X然后顺着天数往下推 貌似也可以
DO IT YOURSELF !
#include <stdio.h> //获取公历年初至某整月的天数 int year_sumday(int year,int month) { int sum=0; int rui[12]={31,29,31,30,31,30,31,31,30,31,30,31}; int ping[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int ruiflag=0; if((year%4==0 &&year%100!=0) || year%400==0) ruiflag=1; for(int index=0;index<month-1;index++) { if(ruiflag==1) sum+=rui[index];else sum+=ping[index]; } return sum; } //获取公历年的天数 int year_alldays(int year) { if((year%4==0 && year%100!=0) || year%400==0) return 366; else return 365; } //获取从公历1975年1月1日至当前日期的总天数 int get_g_alldays(int year,int month,int day) { int i=1975,days=0; while(i<year) { days+=year_alldays(i); i++; } int days2=year_sumday(year,month); return days+days2+day; } int main(int argc, char** argv) { int winit=3; //初始化星期三 1975.01.01 int days=0; int smonth; int _week; int syear=2015; int sday=13; for(smonth=1;smonth<=12;smonth++) { days=get_g_alldays(syear,smonth,sday); _week=(days % 7 + winit-1)%7; if(_week==5) printf("%d年%d月%d日是黑色的星期五\n",syear,smonth,sday); } return 0; }
#include <stdio.h> //利用蔡勒公式计算星期几 //w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1 int getweek(int year,int month,int day) { int w; //星期 int c; //世纪-1 YYYY的头两位 int y; //年份 YYYY的后两位 int m; //月份 >=3 1月 2月看成上年的13月 14月 int d=day; //日 if(month>=3) { c=year / 100; y=year % 100; m=month; } else { m=month+12; y=(year-1) % 100; c=(year-1) / 100; } w=y+y/4+c/4-2*c+(26*(m+1))/10+d-1; w%=7; return w; } int main(int argc, char** argv) { int smonth; int _week; int syear=2015; int sday=13; for(smonth=1;smonth<=12;smonth++) { _week=getweek(syear,smonth,sday); if(_week==5) printf("%d年%d月%d日是黑色的星期五\n",syear,smonth,sday); } return 0; }
#include<stdio.h> int getWeek(int year, int month, int day) { const int c[] = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; int s; s = c[month] + day + ((year % 4 == 0 && year % 100 || year % 400 == 0) && month > 2); year--; s += year * 365 + year / 4 - year / 100 + year / 400; return s % 7; } int cal(int year) { int i, c; for(c = 0, i = 1; i <= 12; c += getWeek(year, i++, 13) == 5); return c; } int main() { int y; scanf("%d", &y); printf("%d", cal(y)); return 0; }