九度教程第9题

题目:

给出年分m和一年中的第n天,算出第n天是几月几号。
输入描述:
输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。
输出描述:
可能有多组测试数据,对于每组数据,
按 yyyy-mm-dd的格式将输入中对应的日期打印出来。
示例1
输入
2000 3
2000 31
2000 40
2000 60
2000 61
2001 60
输出
2000-01-03
2000-01-31
2000-02-09
2000-02-29
2000-03-01
2001-03-01
 

我的思路

//给出天数,算出年月日

#include <stdio.h>
#define ISYEAP(x) (x % 4 == 0 && x % 100 != 0 || x % 400 == 0 ? 1 : 0)

int dayOfMonth[13][2]
{
	0,0,
	31,31,
	28,29,
	31,31,
	30,30,
	31,31,
	30,30,
	31,31,
	31,31,
	30,30,
	31,31,
	30,30,
	31,31
		
};

struct Date
{
	int Day;
	int Month;
	int Year;
	void nextDay()
	{
		Day++;
		if(Day > dayOfMonth[Month][ISYEAP(Year)])
		{
			Day = 1;
			Month++;
			if(Month > 12)
			{
				Month = 1;
				Year++;
			}
		}
	}
};
int buf[3001][13][32];
int main()
{
	Date tmp;
	tmp.Day = 1;
	tmp.Month = 1;
	tmp.Year = 0;
	int cnt = 0;
	while(tmp.Year != 3001)
	{
		buf[tmp.Year][tmp.Month][tmp.Day] = cnt;
		cnt++;
		tmp.nextDay();	
	}
	
	int y, days;
	while(scanf("%d%d", &y, &days) != EOF)
	{
		for(int m = 1; m <= 12; m++)
		{
			if(dayOfMonth[m][ISYEAP(y)] < days)
			{
				days -= dayOfMonth[m][ISYEAP(y)];
			}
			else
			{
				
				printf("%d-%02d-%02d\n", y, m, days);   //这个控制格式
			
				break;
			}
		}
	}
	return 0;
 } 

 

适配器模式(Adapter Pattern)是一种结构型设计模式,它允许将一个类的接口转换成客户端所期望的另一个接口。它通常用于解决两个已有接口之间不兼容的问题。 在给出的代码示例中,我们可以看到适配器模式的应用。在Main.cpp文件中,创建了一个Target对象指针target,并将其初始化为Adapter对象。然后调用target的request()函数,实际上调用的是Adapter的request()函数。而Adapter的request()函数内部调用了Adaptee的specificRequest()函数,完成了适配的过程。 在Head.h文件中定义了三个类:Target、Adaptee和Adapter。Target类是适配器模式中的目标接口,定义了一个虚函数request()。Adaptee类是被适配的类,它有一个特殊的请求函数specificRequest()。Adapter类是适配器类,它继承了Target类,并在其request()函数中调用了Adaptee类的specificRequest()函数。 通过适配器模式,我们可以将不兼容的两个接口进行适配,使它们能够协同工作。这在软件开发中经常用于复用已有代码或集成多个系统。 参考: C++设计模式之适配器模式Adapter Head.cpp Main.cpp<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++设计模式之适配器模式(Adapter)](https://download.youkuaiyun.com/download/weixin_38666785/12761879)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C++设计模式-适配器模式](https://blog.youkuaiyun.com/qq78442761/article/details/95766831)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值