给定年份和月份,返回这个月的天数。
1≤year≤10000
1 \leq month \leq 121≤month≤12
【触碰到我的知识盲区了属于是//2月天数:平年有28天,闰年有29天。】
public class Solution {
/**
* @param year: a number year
* @param month: a number month
* @return: Given the year and the month, return the number of days of the month.
*/
public int getTheMonthDays(int year, int month) {
// write your code here
int days;
if(year<1||year>10000||month<1||month>12)
return -1;
else
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
days=31;
else if(month!=2)
days=30;
else
if (year%400==0)
days=29;
else
if(year%4==0&&year%100!=0)
days=29;
else
days=28;
}
return days;
}
}
判断给出的年份 n
是否为闰年. 如果 n
为闰年则返回 true
闰年是包含额外一天的日历年. 如果年份可以被 4 整除且不能被 100 整除 或者 可以被 400 整除
, 那么这一年为闰年.
public class Solution {
/**
* @param n: a number represent year
* @return: whether year n is a leap year.
*/
public boolean isLeapYear(int n)
{
// write your code here
if (n%400==0)
return true;
else
if(n%4==0&&n%100!=0)
return true;
else
return false;
}
}