题解 | 计算一年中的第几天 c#

计算一年中的第几天

https://www.nowcoder.com/practice/178aa3dafb144bb8b0445edb5e9b812a

using System;
using System.Collections.Generic;
using System.Linq;
public class Program {
    public static void Main() {
        string input = null;
        List<int> daysInMonth = new List<int> { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        while((input = Console.ReadLine()) != null)
        {
            string[] parts = input.Split(' ');
            int year = int.Parse(parts[0]);
            int month = int.Parse(parts[1]);
            int day = int.Parse(parts[2]);
            bool isLeapYear = DateTime.IsLeapYear(year);
            for(int i = 0; i< month - 1; i++)
            {
                day += daysInMonth[i];
            }
            if(isLeapYear && month > 2)
            {
                day += 1;
            }
            Console.WriteLine(day);
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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