判断闰年及相关题

您所在的位置:网站首页 2008年是闰年它之后的两个闰年是多少年和多少年 判断闰年及相关题

判断闰年及相关题

2024-07-16 22:09| 来源: 网络整理| 查看: 265

 首先我们需要知道闰年的判断方法,一是年份能整除于4且不能整除100, 二是能够整除400,这是应该作为常识记住的。

1,输入年份,判断是否式闰年。用函数实现 2,输入年月,输出这一年这一月是多少天。   如: input  2008 2            output  29 3.输入年月日,输出是这一年的第几天。  如: input   2019 3 3          output  62

本文用数组代替了循环,用空间换时间,使代码更加高效,数组定义加了static const为了使代码更加安全,第二个函数数组中0号下标存放闰年二月的值,其它为平年每个月的天数,第三个函数数组中存放月数相加的天数,比如想知道2019年3月3日的是这一年的天数,数组中直接找出前两个月的天数59,判断它不是闰年,不用加一,直接加上它的天数3,就是62天。

#include bool Is_Leap(int year) { return(year % 4 == 0 && year % 100 != 0) ||(year % 400 ==0); } int Get_YMD(int year,int month) { static const int days[13] = {29,31,28,31,30,31,30,31,31,30,31,30,31}; if(2 == month && Is_Leap(year)) { month = 0; } return days[month]; } int Get_Total(int year,int month,int day) { static const int total[] = {0,0,31,59,90,120,151,181,212,243,273,304,334,365}; if(year < 1) return -1; if(month < 1 || month > 12) return -2; if(day < 1 || day >Get_YMD(year,month)) return -3; int sum = total[month] + day; if(month > 2 && Is_Leap(year)) { sum += 1; } return sum; } int main() { int year = 0, month = 0, day = 0; int total = 0; scanf("%d %d %d",&year,&month,&day); total = Get_Total(year,month,day); printf("%d year %d month %d day => %d total\n",year,month,day,total); return 0; }

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3