题解 | #打印日期#

打印日期

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
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务