首页 > 试题广场 >

打印日期

[编程题]打印日期
  • 热度指数:45522 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
给出年分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
def IsLeapYear(year):
    if (year % 4 == 0 and year % 100 != 0) or (year % 400) == 0:
        return 1
    else:
        return 0


month_day = [
    [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
    [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
]
while True:
    try:
        year, num = map(int, input().split())
        row = IsLeapYear(year)
        month = 0
        while num > month_day[row][month]:
            num -= month_day[row][month]
            month += 1
        day = num
        print("%d-%02d-%02d" % (year, month + 1, day))
    except:
        break
发表于 2022-08-01 16:04:55 回复(0)
while True:
    try:
        year,day=map(int,input().strip().split(' '))
        def isrunnian(year):
            if year%4==0 and year%100!=0:
                return True
            elif year%400==0:
                return True
            else:
                return False
        list1=[31,28,31,30,31,30,31,31,30,31,30,31]
        list2=[31,29,31,30,31,30,31,31,30,31,30,31]
        sum1=0
        if isrunnian(year):
            for i in range(1,13):
                sum1=sum(list2[0:i])
                if sum1>=day:
                    month=i
                    date=day-(sum1-list2[i-1])
                    break
        else:
            for i in range(1,13):
                sum1=sum(list1[0:i])
                if sum1>=day:
                    month=i
                    date=day-(sum1-list1[i-1])
                    break
        result=[str(year)]
        if len(str(month))<2:
            result.append('0'+str(month))
        else:
            result.append(str(month))
        if len(str(date))<2:
            result.append('0'+str(date))
        else:
            result.append(str(date))
        print('-'.join(result))

    except:
        break
发表于 2019-08-22 08:56:18 回复(0)

使用python的datetime库,美滋滋。

import datetime

while True:
    try:
        year, day = map(int, input().split())
        first_day = datetime.datetime(year, 1, 1)
        delta = datetime.timedelta(days=day - 1)
        print(datetime.datetime.strftime(first_day + delta, "%Y-%m-%d"))


    except:
        break
发表于 2017-10-04 08:50:08 回复(0)
import sys

months = {0:0,1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}

def sum_month(num):
    sum_days = 0
    for n in range(1,num+1):
    sum_days += months[n]
    return  sum_days

for line in sys.stdin:
    years,days = line.split(' ')
    year = int(years)
    days = int(days)
    
    if year%4==0 and year%100 != 0:
        months[2] = 29
    elif year%4 == 0 and year%100 == 0 and year%400 == 0:
        months[2] = 29
    else:
        months[2] = 28
            
    for mon in range(1,13):
        if sum_month(mon) >= days:
            month = mon
            day = days - sum_month(mon-1)
            break

    print '%4d-%02d-%02d' % (year,month,day)

发表于 2017-02-19 15:24:03 回复(0)
import datetime as D

while True:
    try:
        parts = map(int, raw_input().split())
        print (D.datetime(parts[0], 1, 1) + D.timedelta(parts[1] - 1)).strftime('%Y-%m-%d')
    except EOFError:
        break
in java:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

/**
 * Created by fhqplzj on 17-1-6 at 下午7:52.
 */
public class Main {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int year = scanner.nextInt();
            int delta = scanner.nextInt();
            calendar.set(year, 0, 0);
            calendar.add(Calendar.DAY_OF_YEAR, delta);
            System.out.println(simpleDateFormat.format(calendar.getTime()));
        }
    }
}

编辑于 2017-01-06 20:03:36 回复(0)
import datetime as D
try:
    while 1:
        a = map(int, raw_input().split())
        print (D.datetime(a[0] - 1,12,31) + D.timedelta(days=a[1])).strftime('%Y-%m-%d')
except:
    pass

发表于 2016-12-23 12:05:31 回复(0)