闰年的定义和程序计算

学人智库 时间:2018-01-15 我要投稿
【meiwen.anslib.com - 学人智库】

天文专家表示,农历鸡年是个闰年,有一个“闰6月”,共有6个小月,每月29天和7个大月,每月30天,一年共有384天。

定义

①、普通年能整除4且不能整除100的为闰年.(如2004年就是闰年,1900年不是闰年)

②、世纪年能整除400的是闰年.(如2000年是闰年,1900年不是闰年)

③、对于数值很大的年份,这年如果能被3200整除,并且能被172800整除则是闰年.如172800年是闰年,86400年不是闰年(因为虽然能被3200整除,但不能被172800整除)

程序计算

Ecmascript语言:

1234567    // 判断指定年份是否为闰年       function isleap(){           var the_year = new Date().getFullYear();           var isleap = the_year % 4 == 0 && the_year % 100 !=0 || the_year % 400 ==0;           return isleap;       }        

C#语言:

123456789        /// <summary>        /// 判断指定年份是否为闰年        /// </summary>        /// <param name="year">年份</param>        /// <returns>返回布尔值true为闰年,反之不是</returns>        public static bool isLeapYear(int year)        {            return ((year % 4 == 0 && year % 100 != 0) ||year%400==0);        }

Java语言:

12345678910111213import java.util.Scanner;public class LeapYear {    public static void main(String[] args) {            Scanner input = new Scanner(System.in);        System.out.print("请输入年份:");        int year = input.nextInt();                 if((year % 4 == 0 && year % 100 != 0) || year % 400 ==0)            System.out.print(year + "年是闰年。");        else             System.out.print(year + "年不是闰年。");    }}

VB语言:

123Public Function isLeapYear(year As Integer) As BooleanisLeapYear = (year Mod 4 = 0 And year Mod 100 <> 0) Or year Mod 400 = 0End 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语言:

12345function lpflag = isleapyear(year)% 判断是否为闰年% Input  -year 年份,数值% Output -lpflag lpflag = 1,闰年;lpflag = 0,平年lpflag = (~mod(year, 4) && mod(year, 100)) || ~mod(year, 400);

Erlang语言:

123456789101112-module(year).-export([isLeap/1]).isLeap(Year) ->    if Year rem 400 == 0 ->        true;    Year rem 100 == 0 ->        false;    Year rem 4 == 0 ->        true;    true ->        false    end.

Bash/Shell:

1234567year=$1if [ "$(( $year % 4 ))" == "0" ] && [ "$(( $year % 100 ))" != "0" ] || [ "$(( $year % 400 ))" == "0" ]then    echo "leap year"else    echo "common year"fi

[闰年的定义和程序计算]