好的..那么问题就是..假设它是2001年1月1日的星期一所以......作为参考......一年的键盘输入,找出1号的那一天是什么日子那个(输入)年的1月。
在我的程序中(下面)..当人们输入大于参考年份(2001)的输入时..答案是正确的但是..如果输入小于2001 ..答案是错误的。 。 能否指出并解释我的代码中的错误.. 感谢..
#include <stdio.h>
#include<math.h>
#include <stdlib.h>
int main()
{
int present_year;
int normal_days;
int normal_year;
int leap_year;
int leap_days;
int check;
int total_days;
int reference_year=2001;
int day;
printf("Enter the year you want to check\n");
scanf("%d",&present_year);
if(reference_year<present_year)
/*if year entered is greater than reference year(2001)*/
{
check=present_year-reference_year;
}
if(present_year<reference_year)
/* if year entered is smaller than reference year*/
{
check=reference_year-present_year;
}
leap_year=check/4;
normal_year=check-leap_year;
normal_days=normal_year*365;
leap_days=leap_year*366;
total_days=leap_days+normal_days;
day=total_days%7;
if(day==0)
printf("January 1 of year %d will be Monday\n",present_year);
if(day==1)
printf("January 1 of year %d will be Tuesday\n",present_year);
if(day==2)
printf("January 1 of year %d will be Wednesday\n",present_year);
if(day==3)
printf("January 1 of year %d will be Thursday\n",present_year);
if(day==4)
printf("January 1 of year %d will be Friday\n",present_year);
if(day==5)
printf("January 1 of year %d will be Saturday\n",present_year);
if(day==6)
printf("January 1 of year %d will be Sunday\n",present_year);
return 0;
}
答案 0 :(得分:1)
从哪里开始...你甚至了解闰年是什么?我不认为你这样做。它每隔四年不。
在这里,试试这个。
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
double mod(double x, double y)
{
return x - y * floor(x / y);
}
bool gregorian_leap_year(int year)
{
return (
mod(year, 4) == 0 &&
!(mod(year, 400) == 100 ||
mod(year, 400) == 200 ||
mod(year, 400) == 300)) ? true : false;
}
int fixed_from_gregorian(int year, int month, int day)
{
int correction, f;
if (month <= 2) correction = 0;
else if (month > 2 && gregorian_leap_year(year)) correction = -1;
else correction = -2;
f = 365 * (year - 1) +
floor((year - 1) / 4.0) -
floor((year - 1) / 100.0) +
floor((year - 1) / 400.0) +
floor((367 * month - 362) / 12.0) +
correction + day;
return f;
}
char *daynames[] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
int main(int argc, char *argv[])
{
int present_year;
printf("Enter the year you want to check\n");
scanf("%d", &present_year);
int f = fixed_from_gregorian(present_year, 1, 1);
int day = (int)mod(f,7);
printf("January 1 of year %d will be %s\n", present_year, daynames[day]);
return 0;
}
答案 1 :(得分:0)
我不知道为什么你在2001年1月1日进行了初始化,而你可以在1月1日(即星期一)初始化它。然后只需找到奇数天并计算1月1日XXXX的日期
odd_days=(year-1) + leap_years;
然后
day=odd_days%7;