题解 | #获得月份天数#

获得月份天数

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");
            };
        }
    }
}

全部评论

相关推荐

头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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