算法训练:黑色星期五

7 篇文章 0 订阅

题目描述:

13号又是一个星期五。13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数。给出N年的一个周期,要求计算1900年1月1日至1900+N-1年12月31日中十三号落在周一到周日的次数,N为正整数且不大于400.

这里有一些你要知道的:

1、1900年1月1日是星期一.

2、4,6,11和9月有30天.其他月份除了2月都有31天.闰年2月有29天,平年2月有28天.

3、年份可以被4整除的为闰年(1992=4*498 所以 1992年是闰年,但是1990年不是闰年).

4、以上规则不适合于世纪年。可以被400整除的世纪年为闰年,否则为平年。所以,1700,1800,1900和2100年是平年,而2000年是闰年.

请不要调用现成的函数

请不要预先算好数据(就是叫不准打表)!

输入输出格式
输入格式:
一个正整数n.

输出格式:
**输出请从周六开始

C实现

#if 0
//一般方法:
#include<stdio.h>
#include<windows.h>
#pragma warning (disable:4996)

int Judge_IsLeapYear(int year)
{
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
		return 1; //是闰年
	}
	return 0; //是平年
}

int Judge_Month(int year, int month)
{
	if (2 == month){
		if (Judge_IsLeapYear(year) == 1){//闰年2月
			return 29;
		}
		else{//平年2月
			return 28;
		}
	}
	if (month == 4 || month == 6 || month == 9 || month == 11){
		return 30;//小月
	}
	else{
		return 31;//大月
	}

}

void WeekCount(int week[], int total_days, int cur_month)
{
	int cur_13 = total_days - (cur_month - 13);
	switch (cur_13 % 7){
		case 0:week[1]++; break;
		case 1:week[2]++; break;
		case 2:week[3]++; break;
		case 3:week[4]++; break;
		case 4:week[5]++; break;
		case 5:week[6]++; break;
		case 6:week[0]++; break;
		default:break;
	}
}

int main()
{
	int total_days = 0;
	int week[7] = { 0 }; //week[0]:星期六 week[1]:星期日 week[2]:星期一 ......
	int year = 1900;
	int month = 1;

	int n = 0;
	printf("请输入要查询截止的年份:");
	scanf("%d", &n);

	for (; year < 1900 + n; year++){
		for (month = 1; month <= 12; month++){
//			printf("%d:%d----", year, month);
			int cur_month = Judge_Month(year, month);
//			printf("%d\t\t\t", cur_month);
			total_days += cur_month;

			WeekCount(week, total_days, cur_month);
//			printf("%d\n", total_days);
			/*
			printf("\n");
			for (int i = 0; i < 7; i++){
				printf("%d ", week[i]);
			}
			printf("\n");
			*/

		}
	}


	//输出
	printf("\n\n\n");
	for (int i = 0; i < 7; i++){
		printf("%d ", week[i]);
	}

	system("pause");
	return 0;
}

#else
//简化方法:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>

int judge(int year)
{
	if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
		return 1;
	return 0;
}
int main()
{
	int n;
	int week[7] = { 0 };
	int i, j;
	int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int weekday = 5;         //1900.1.13是星期六
	week[5] = 1;
	scanf("%d", &n);
	for (i = 1900; i <= 1900 + n - 1; i++)
	{
		for (j = 1; j <= 12; j++)
		{
			if (judge(i) == 1 && j == 2)
			{
				weekday = (weekday + 29) % 7;
				week[weekday]++;
			}
			else
			{
				weekday = (weekday + days[j]) % 7;
				week[weekday]++;
			}
		}
	}
	week[weekday]--;
	for (int i = 0; i < 7; i++)
	{
		printf("星期%d:%d天\n", i + 1, week[i]);
	}
	system("pause");
	return 0;
}

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值