日期问题九度1070+1186

题目1070:今年的第几天?

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:3491

解决:1936

题目描述:

输入年、月、日,计算该天是本年的第几天。

输入:

包括三个整数年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。

输出:
输入可能有多组测试数据,对于每一组测试数据,
输出一个整数,代表Input中的年、月、日对应本年的第几天。
样例输入:
1990 9 20
2000 5 1
样例输出:
263
122
#include <iostream>
#include<stdio.h>
#define isyear(x) x%4==0&&x%100!=0||x%400==0?1:0
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
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 year;
	int day;
	int month;
	void nextday()
	{
		day++;
		if(day>dayofmonth[month][isyear(year)])
		{
			day=1;
			month++;
		}
		if(month>12)
		{
			month=1;
			year++;
		}
	};
};
int buf[3001][13][32];


int main(int argc, char** argv) {
	date tmp;
	tmp.day=1;
	tmp.month=1;
	tmp.year=0;
	int count=0;
	while(tmp.year<3001)
	{
		buf[tmp.year][tmp.month][tmp.day]=count;
		tmp.nextday();
		count++;
	}
	int y,m,d;
	while(scanf("%4d %2d %2d",&y,&m,&d)!=EOF)
	{
		printf("%d",buf[y][m][d]-buf[y][1][1]+1);
	}
	return 0;
}

九度OJ—题目1186:打印日期

给出年分m和一年中的第n天,算出第n天是几月几号。

输入:

输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。

输出:

可能有多组测试数据,对于每组数据, 按 yyyy-mm-dd的格式将输入中对应的日期打印出来。

样例输入:
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
来源:
2003-2005年华中科技大学计算机研究生机试真题
#include <iostream>
#include<stdio.h>
#define isyear(x) x%4==0&&x%100!=0||x%400==0?1:0
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
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 year;
int day;
int month;
void nextday()
{
day++;
if(day>dayofmonth[month][isyear(year)])
{
day=1;
month++;
if(month>12)
{
month=1;
year++;
}
}


};
};
int buf[3001][13][32];
int main(int argc, char** argv) {
date tmp;
tmp.day=1;
tmp.year=0;
tmp.month=1;
int count=0;
while(tmp.year<3001)
{
buf[tmp.year][tmp.month][tmp.day]=count;
tmp.nextday();
count++;
}
int y,d,m;
int totald;
while(scanf("%d %d",&y,&totald)!=EOF)
{
m=1,d=0;
for(int i=0;i<totald;i++)
{
if(buf[y][m][d]-buf[y][1][1]+1==0)//好吧,我感觉是在暴力破解 
break;
d++;
if(d>dayofmonth[m][isyear(y)])
{
d=1;
m++;
}
}
printf("%04d-%02d-%02d\n",y,m,d);
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值