编写C语言程序:黑色星期五

编写一个程序,统计出在某个特定的年份中,出现了多少次既是13号又是星期五的情形。说明:(1)一年有365天,闰年有366天,所谓闰年,即能被4整除且不能被100整除的年份,或是既能被100整除也能被400整除的年份;(2)已知1998年1月1日是星期四,用户输入的年份肯定大于或等于1998年。
输入格式:输入只有一行,即某个特定的年份(大于或等于1998年)。
输出格式:输出只有一行,即在这一年中,出现了多少次既是13号又是星期五的情形。

#include <stdio.h>
int normalMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int leapMonth[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
int getYear(void)
{
int n;
printf("please input year:");
scanf("%d",&n);
return n;
}

int isLeap(int n)
{
if((n%4==0&&n%100!=0)||n%400==0)
{
return 366;
}
else
{
return 365;
}
}

int weekDays(int year)
{
int i,sum = 0;
for(i = 2008;i<year;i++)
{
sum += isLeap(i);
}
if(sum == 0)
{
sum = 0;
}
else
{
sum = sum%7;
}
switch (sum)
{
case 0:
return 3;
break;
case 1:
return 4;
break;
case 2:
return 5;
break;
case 3:
return 6;
break;
case 4:
return 7;
break;
case 5:
return 1;
break;
case 6:
return 2;
break;
default:
return -10000;
}
}

void darkFriday(int weekday,int year)
{
int week = weekday,i,j,sum = 0;
if(366==isLeap(year))
{
for(i=0;i<12;i++)
{
for(j=1;j<=leapMonth[i];j++)
{
week++;
if(week == 8)
{
week = 1;
}
if(week == 5&& j == 13)
{
sum ++;
}
}
}
}
else
{
for(i=0;i<12;i++)
{
for(j=1;j<=normalMonth[i];j++)
{
week++;
if(week == 8)
{
week = 1;
}
if(week == 5&& j == 13)
{
sum ++;
}
}
}
}
printf("%d\n",sum);
}
int main(void)
{
int year,weekday;
year = getYear();
weekday = weekDays(year);
darkFriday(weekday,year);
return 0;
}
可能不对,你看看吧。楼上的可能更好。
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-12-15
#include <stdio.h>
void main()
{
int y, m, w;
int count = 0;
scanf("%d", &y);
int c=y/100;
y= y - c * 100; //分离世纪与年
for (m = 1; m <= 12; ++m)
{
w=y+y/4+c/4-2*c+26*(m+1)/10+13-1; // 蔡勒公式
if (w % 7 == 5)
{
//printf("%d月\n", m);
count++;
}
}
printf("一共%d个黑色星期五\n", count);
}本回答被提问者采纳
相似回答