c语言编程题 已知2012年1月1日是星期日,现在输入其后的任意一个日期(年份不超过9999), 计算该日是星期几.

2024-11-26 10:30:45
推荐回答(3个)
回答1:

typedef struct DATE
{
int year;
int month;
int day;
}DATE;
DATE StanderdDate{2012,1,1};//标准日期
int StanderdDayOfWeek =6;//标准日期是周几 //周日
bool isRuiYear(int year)//判断是否闰年
{
if((year % 400 == 0)|(year % 4 == 0)&(year % 100 != 0))
return true;
else
return false;
}

int SumDays(DATE date)
{
if(date.)
int sum=0;//日期天数差值
int i=0;//循环变量
for(i=0;i {
if(isRuiYear(StanderdDate.year+i))//判断是否闰年
sum+=366;
else
sum+=365;
};
int mon[12]={31,28,31,30,31,30,31,31,30,31,30,31};//非闰年
for(i=0;i {
sum+=mon[StanderdDate.month+i];
if(i==1)
{
if(isRuiYear(date.year))
sum+=1;//润年+1
}
};
sum+=date.day-StanderdDate.day;//
return (sum+StanderdDayOfWeek)%7;//返回星期几
}

回答2:

#include"stdio.h"
typedef struct {
int year;
int month;
int day;
}Date;
bool IsLeapYear(int year)
{
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
return true;
return false;
}
else
return true;
}
else
return false;
}
int days(Date date)
{
int weekday=7;
for(int year=2012;year if(IsLeapYear(year))
weekday=(weekday+2)%7;
else
weekday=(weekday+1)%7;

for(int month=1;month if(month==1 || month==3 ||month==5 ||month==7 ||month==8 ||month==10 ||month==12 )
weekday=(weekday+3)%7;
else
if(month==4 || month==6 ||month==9 ||month==11 )
weekday=(weekday+2)%7;
else
if(IsLeapYear(year))
weekday=(weekday+1)%7;

for(int day=1;day ;
weekday=(weekday+day-1)%7;

return weekday;
}
void main()
{
Date date;
scanf("%d%d%d",&date.year,&date.month,&date.day);//依次输入年,月,日
printf("%d",days(date));
}

回答3:

去网上找