黑色星期五

本文提供了一段C++代码,用于找出九十年代中黑色星期五计算机病毒可能发作的所有日期。该程序通过输入当前日期,并计算出在此基础上所有符合条件的日期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/************************************************************************/
/*题目:有一种计算机病毒叫黑色星期五,如果当天是13号,又恰好是星期五,就会
/*      发作起来毁坏计算机存储系统。试编程找出九十年代中这种病毒可能发作的
/*      日期。                                                                  
/************************************************************************/

#include <iostream>

using namespace std;

int main()
{
	int nYear;  //input year
	int nMonth; //input month
	int nDay;   //input day
	int nWeekDay; //input the day of week

	cout << "Enter the date of today in the form of: year month day dayofweek : " << endl;
	cin >> nYear >> nMonth >> nDay >> nWeekDay;

	//
	switch (nWeekDay) //先保证是星期五,相应天数要调整
	{
	case 1: 
		nDay += 4; break;
	case 2:
		nDay += 3; break;
	case 3:
		nDay += 2; break;
	case 4:
		nDay += 1; break;
	case 6:
		nDay -= 1; break;
	case 7:
		nDay -= 2; break;
	}
	//
    cout << "The date may be as following: " << endl;
	while (nYear >= 1990)
	{
		while (nDay >= 7)
		{
			if (nDay == 13 && nYear >= 1990 && nYear <= 1999)
				cout << nYear << "年 " << nMonth << "月 " << nDay << "日 " << " 星期五" << endl;
		    nDay -= 7;
		}

		switch (--nMonth) //先倒退一个月,以计算前一个月的天数
		{
		case 1: case 3: case 5: case 7:
		case 8: case 10: case 12:
			nDay += 31;		
			break;
		case 4: case 6: case 9: case 11:
			nDay += 30;
			break;
		case 2:
			if ((nYear % 4 == 0) && (nYear % 100) || (nYear % 400 == 0))
				nDay += 29; //闰年2月有29天
			else
				nDay += 28; //平年2月28天
			break;
		case 0:             //倒退一年
			nYear -= 1;
			nMonth = 12;
			break;
		}
	}
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值