範文齋

位置:首頁 > 校園範文 > 教材

閏年的定義和程序計算

教材1.13W

天文專家表示,農曆雞年是個閏年,有一個“閏6月”,共有6個小月,每月29天和7個大月,每月30天,一年共有384天。

閏年的定義和程序計算
定義

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

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

③、對於數值很大的年份,這年如果能被3200整除,並且能被172800整除則是閏年.如172800年是閏年,86400年不是閏年(因爲雖然能被3200整除,但不能被172800整除)

程序計算

Ecmascript語言

1234567//判斷指定年份是否爲閏年functionisleap(){varthe_year=newDate()ullYear();varisleap=the_year%4==0&&the_year%100!=0||the_year%400==0;returnisleap;}

C#語言:

123456789///<summary>///判斷指定年份是否爲閏年///</summary>///<paramname="year">年份</param>///<returns>返回布爾值true爲閏年,反之不是</returns>publicstaticboolisLeapYear(intyear){return((year%4==0&&year%100!=0)||year%400==0);}

Java語言:

12345678910111213importner;publicclassLeapYear{publicstaticvoidmain(String[]args){ Scannerinput=newScanner();t("請輸入年份:");intyear=Int();if((year%4==0&&year%100!=0)||year%400==0)t(year+"年是閏年。");elset(year+"年不是閏年。");}}

VB語言:

123PublicFunctionisLeapYear(yearAsInteger)AsBooleanisLeapYear=(yearMod4=0AndyearMod100<>0)OryearMod400=0EndFunction

Python 語言:

1234567#-*-coding:cp936-*-temp=input("輸入年份:")YEAR=int(temp)if(YEAR%4==0andYEAR%100!=0)orYEAR%400==0:print("閏年")else:print("非閏年")

C++語言:

123456789#include<iostream>intmain(){intyear;std::cout<<"請輸入年份:";std::cin>>year;//輸入待判斷年份,如2008std::cout<<year<<(((year%4==0&&year%100!=0)||year%400==0)==1?"年是閏年":"年是平年")<<std::endl;return0;}

C語言:

123456789101112#include<stdio.h>intmain(void){inty;printf("請輸入年份,回車結束");scanf("%d",&y);if((y%4==0&&y%100!=0)||y%400==0)printf("%d是閏年",y);elseprintf("%d是平年",y);return0;}

MATLAB語言:

12345functionlpflag=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)->ifYearrem400==0->true;Yearrem100==0->false;Yearrem4==0->true;true->falseend.

Bash/Shell:

1234567year=$1if["$(($year%4))"=="0"]&&["$(($year%100))"!="0"]||["$(($year%400))"=="0"]thenecho"leapyear"elseecho"commonyear"fi

標籤:閏年 計算