题目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
-
#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;
} -