题解 | #获得月份天数#

获得月份天数

https://www.nowcoder.com/practice/13aeae34f8ed4697960f7cfc80f9f7f6

public class Program {
    public static void Main() {
        //大月(1,3,5,7,8,10,12)是31天,小月(4,6,9,11)是30天
        //2月份特殊处理,闰年是29天,非闰年是28天
        //闰年的判断方法:(四年一闰&&百年不闰)||(四百年再一闰)

        string lines;
        while ((lines = System.Console.ReadLine()) != null) {
            string[] line = lines.Split(" ");

            int year = int.Parse(line[0]);
            int month = int.Parse(line[1]);

            //不是2月份的情况
            if (month != 2) {
                //判断大月还是小月
                if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||
                        month == 10 || month == 12)
                    System.Console.WriteLine("31");
                else
                    System.Console.WriteLine("30");
            }
            //是2月份的情况要判断年份是否为闰年
            else if (month == 2) {
                //是闰年的情况下
                if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
                    System.Console.WriteLine("29");
                else
                    System.Console.WriteLine("28");
            };
        }
    }
}

全部评论

相关推荐

找个工作 学历是要卡的 要求是高的 技能不足是真的 实习经验是0的 简历无处可写是事实的 钱不好赚是真的 想躺平又不敢躺 也不甘心躺 怕自己的灵感和才华被掩埋甚至从未被自己发现 又质疑自己是否真正有才华
码农索隆:你现在啊,你心里都明白咋回事,但是你没办法改变现状,一想到未来,你又没有信心狠下心来在当下努力。 得走出这种状态,不能一直困在那里面,哪不行就去提升哪,你一动不动那指定改变不了未来,动起来,积少成多才能越来越好
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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