1. 首頁
  2. 教材

閏年的定義和程式計算

閏年的定義和程式計算

天文專家表示,農曆雞年是個閏年,有一個“閏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 IntegerAs 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 != 0or 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