题解 | #打印日期#

打印日期

https://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b

//C++版代码
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int year, totalDay;
    int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    while (cin >> year >> totalDay) {
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) days[2] = 29;
        else days[2] = 28;
        int curDay = 1, month = 1;
        while (--totalDay) {
            curDay++;
            if (curDay > days[month]) {
                curDay = 1;
                month++;
            }
        }
        cout << year << '-' << setw(2) << setfill('0') << month << '-' << setw(
                 2) << setfill('0') << curDay << endl;
    }
    return 0;
}
//Java版代码
import java.time.LocalDate;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int year = sc.nextInt();
            int day = sc.nextInt();
            System.out.println(LocalDate.ofYearDay(year, day));
        }
    }
}
//Python版代码
from datetime import datetime, timedelta
while True:
    try:
        year, day = map(int, input().split())
        print((datetime(year, 1, 1) + timedelta(days=day - 1)).strftime('%Y-%m-%d'))
    except:
        break

全部评论

相关推荐

点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务