闰年的定义和程序计算
来源:才华屋 阅读:1.22W 次
天文专家表示,农历鸡年是个闰年,有一个“闰6月”,共有6个小月,每月29天和7个大月,每月30天,一年共有384天。
定义①、普通年能整除4且不能整除100的`为闰年.(如2004年就是闰年,1900年不是闰年)
②、世纪年能整除400的是闰年.(如2000年是闰年,1900年不是闰年)
③、对于数值很大的年份,这年如果能被3200整除,并且能被172800整除则是闰年.如172800年是闰年,86400年不是闰年(因为虽然能被3200整除,但不能被172800整除)
程序计算Ecmascript语言:
1234567 |
//判断指定年份是否为闰年
functionisleap(){
var the_year= new Date()ullYear();
var isleap=the_year%4==0&&the_year%100!=0||the_year%400==0;
return isleap;
}
|
C#语言:
123456789 |
///<summary>
///判断指定年份是否为闰年
///</summary>
///<paramname="year">年份</param>
///<returns>返回布尔值true为闰年,反之不是</returns>
public static bool isLeapYear( int year)
{
return ((year%4==0&&year%100!=0)||year%400==0);
} |
Java语言:
12345678910111213 | import ner; public class LeapYear{
public static void main(String[]args){
Scannerinput= new Scanner();
t( "请输入年份:" );
int year=Int();
if ((year% 4 == 0 &&year% 100 != 0 )||year% 400 == 0 )
t(year+ "年是闰年。" );
else
t(year+ "年不是闰年。" );
} } |
VB语言:
123 | Public Function isLeapYear(year As Integer ) As Boolean isLeapYear=(year Mod 4=0 And year Mod 100<>0) Or year Mod 400=0 End Function |
Python 语言:
1234567 | #-*-coding:cp936-*- temp = input ( "输入年份:" ) YEAR = int (temp) if (YEAR % 4 = = 0 and YEAR % 100 ! = 0 ) or YEAR % 400 = = 0 :
print ( "闰年" ) else :
print ( "非闰年" ) |
C++语言:
123456789 | #include<iostream> int main() {
int year;
std::cout<< "请输入年份:" ;
std::cin>>year; //输入待判断年份,如2008
std::cout<<year<<(((year%4==0&&year%100!=0)||year%400==0)==1? "年是闰年" : "年是平年" )<<std::endl;
return 0; } |
C语言:
123456789101112 | #include<stdio.h> int main( void ) {
int y;
printf ( "请输入年份,回车结束" );
scanf ( "%d" ,&y);
if ((y%4==0&&y%100!=0)||y%400==0)
printf ( "%d是闰年" ,y);
else
printf ( "%d是平年" ,y);
return 0; } |
MATLAB语言:
12345 | functionlpflag=isleapyear(year) %判断是否为闰年 %Input-year年份,数值 %Output-lpflaglpflag=1,闰年;lpflag=0,平年 lpflag=(~mod(year,4)&&mod(year,100))||~mod(year,400); |
Erlang语言:
123456789101112 | -module(year). -export([isLeap/1]). isLeap(Year)->
if Yearrem400==0->
true ;
Yearrem100==0->
false ;
Yearrem4==0->
true ;
true ->
false
end. |
Bash/Shell:
1234567 | year=$1 if [ "$(($year%4))" == "0" ]&&[ "$(($year%100))" != "0" ]||[ "$(($year%400))" == "0" ] then
echo "leapyear" else
echo "commonyear" fi |
热门内容
大家都在看
最近更新