如何调整这段代码的输出:
假设我在 2012 年输入。输出应该是 February 29, 2012 Wednesday is a leap year (same thing with other leap year)
如果我输入 2013,输出应该是 2013 不是闰年。
这是我当前的代码:
include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
闰年的2月29日是星期几怎么判断?
最佳答案
你的问题让我想自己得到它 并开始对其进行研究和编写代码。我找到了一段代码,可以通过输入日期模式返回一天。在您的情况下,如果它是闰年,则日期为 29,2,year,如果不是,则为 28,2,year。这里 0 代表星期日,1 代表星期一,同样 6 代表星期六。
/* A program to find day of a given date */
#include<stdio.h>
int dayofweek(int d, int m, int y)
{
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y -= m < 3;
return ( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
/* Driver function to test above function */
int main()
{
int day = dayofweek(10, 10, 2014);
printf ("%d", day);
return 0;
}
如果您发现代码中有任何错误或难以对其进行更改。请让我知道我会尽力提供帮助。在您的代码中添加此代码可能会满足您的需求。
关于判断闰年的C程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26302793/