How to Determine the Leap Year?

  • 时间:2020-09-21 09:15:21
  • 分类:网络文摘
  • 阅读:144 次

A Leap year occurs mostly every 4 years, but every 100 years, we skip a leap year, unless it is divisible by 400.

Leap Year Algorithm

if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)

How to Test Leap Year in C++?

1
2
3
4
5
6
7
8
9
10
11
bool isLeap(int Y) {
    if (Y % 400 == 0) {
        return true;
    } else if ( Y % 100 == 0) {
        return false;
    } else if (Y % 4 == 0) {
        return true;
    } else {
        return false;
    }        
}
bool isLeap(int Y) {
    if (Y % 400 == 0) {
        return true;
    } else if ( Y % 100 == 0) {
        return false;
    } else if (Y % 4 == 0) {
        return true;
    } else {
        return false;
    }        
}

Or shorter:

1
2
3
bool isLeap(int Y) {
  return (Y % 400 == 0) || ((Y % 100 != 0) && (Y % 4 == 0));
}
bool isLeap(int Y) {
  return (Y % 400 == 0) || ((Y % 100 != 0) && (Y % 4 == 0));
}

How to Test Leap Year in C?

1
2
3
4
5
6
7
8
9
10
11
int isLeap(int Y) {
    if (Y % 400 == 0) {
        return true;
    } else if ( Y % 100 == 0) {
        return false;
    } else if (Y % 4 == 0) {
        return true;
    } else {
        return false;
    }        
}
int isLeap(int Y) {
    if (Y % 400 == 0) {
        return true;
    } else if ( Y % 100 == 0) {
        return false;
    } else if (Y % 4 == 0) {
        return true;
    } else {
        return false;
    }        
}

Or shorter:

1
2
3
int isLeap(int Y) {
  return (Y % 400 == 0) || ((Y % 100 != 0) && (Y % 4 == 0));
}
int isLeap(int Y) {
  return (Y % 400 == 0) || ((Y % 100 != 0) && (Y % 4 == 0));
}

How to Test Leap Year in Java?

1
2
3
4
5
6
7
8
9
10
11
boolean isLeap(int Y) {
    if (Y % 400 == 0) {
        return true;
    } else if ( Y % 100 == 0) {
        return false;
    } else if (Y % 4 == 0) {
        return true;
    } else {
        return false;
    }        
}
boolean isLeap(int Y) {
    if (Y % 400 == 0) {
        return true;
    } else if ( Y % 100 == 0) {
        return false;
    } else if (Y % 4 == 0) {
        return true;
    } else {
        return false;
    }        
}

Or shorter:

1
2
3
boolean isLeap(int Y) {
    return (Y % 400 == 0) || ((Y % 100 != 0) && (Y % 4 == 0));
}
boolean isLeap(int Y) {
    return (Y % 400 == 0) || ((Y % 100 != 0) && (Y % 4 == 0));
}

How to Test Leap Year in Javascript?

1
2
3
4
5
6
7
8
9
10
11
var isLeap = function(Y) {
    if (Y % 400 == 0) {
        return true;
    } else if ( Y % 100 == 0) {
        return false;
    } else if (Y % 4 == 0) {
        return true;
    } else {
        return false;
    }        
}
var isLeap = function(Y) {
    if (Y % 400 == 0) {
        return true;
    } else if ( Y % 100 == 0) {
        return false;
    } else if (Y % 4 == 0) {
        return true;
    } else {
        return false;
    }        
}

Or shorter:

1
2
3
var isLeap = function(Y) {
    return (Y % 400 == 0) || ((Y % 100 != 0) && (Y % 4 == 0));    
}
var isLeap = function(Y) {
    return (Y % 400 == 0) || ((Y % 100 != 0) && (Y % 4 == 0));    
}

How to Test Leap Year in Python/Python3?

1
2
3
4
5
6
7
8
9
def isLeap(Y):
    if Y % 400 == 0:
        return True
    elif Y % 100 == 0:
        return False
    elif Y % 4 == 0:
        return True
    else:
        return False
def isLeap(Y):
    if Y % 400 == 0:
        return True
    elif Y % 100 == 0:
        return False
    elif Y % 4 == 0:
        return True
    else:
        return False

or shorter:

1
2
def isLeap(Y):
    return (Y % 400 == 0) or ((Y % 100 != 0) and (Y % 4 == 0))  
def isLeap(Y):
    return (Y % 400 == 0) or ((Y % 100 != 0) and (Y % 4 == 0))  

All Leap Year Algorithms run at O(1) time and O(1) constant space.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
大家别忘了喝碗营养丰富的腊八粥,它对女性朋友的好处尤其多  经常吃一点柚子好处多,柚子皮的作用也不少,以后别再浪费啦  牛奶是常见的营养饮品,如果选择不对,既浪费钱还影响健康  香蕉对身体健康有很多好处,教你用香蕉做一道美味粥吧  香菇与洋葱搭配在一起营养全面,使得保健功效会更好  分数的运算古代的分数除法  巧用份数解决问题  有趣的迷路问题  华罗庚的回归  不可微——不吃饭 
评论列表
添加评论