这道题只要模拟就行了,求出每个月13日是星期几,然后汇总,输出即可(注意输出顺序!)
求每个月13日是星期几,可以用递推的方法。首先先记录上个月13日是多少天,再计算从上个月13日到这个月13日有几天,这样这个月13日是星期几就很好求出了,而且速度很快。
废话少说,代码上:
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
static int currentDay = -18;
void update(int month,int year)
{
if (month == 2 || month == 4 || month == 6 || month == 8 || month == 9 || month == 11 || month == 1)
{
currentDay += 31;
}
else if (month == 5 || month == 7 || month == 10 ||month == 12)
{
currentDay += 30;
}
else if (year % 4 == 0 && year % 100 != 0)
{
currentDay += 29;
}
else if (year % 400 == 0)
{
currentDay += 29;
}
else currentDay += 28;
}
int main(int argc, const char * argv[])
{
int record[7];
int years;
int i,j;
FILE * input;
FILE * output;
input = fopen("friday.in", "r");
output = fopen("friday.out", "w");
fscanf(input,"%d",&years);
for (i = 0;i != 7;++i) record[i] = 0;
for (i = 1900;i < 1900+years;++i)
{
for (j = 1;j != 13;++j)
{
update(j, i);
record[currentDay % 7]++;
}
}
fprintf(output,"%d",record[6]);
for (i = 0 ;i != 6;++i)
{
fprintf(output," %d",record[i]);
}
fprintf(output, "\n");
fclose(input);
fclose(output);
exit(0);
}