计算闰年的更好算法? [英] A better algorithm to calculate a leap year?

查看:69
本文介绍了计算闰年的更好算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,如果我的风格不正确,请原谅。任何人都可以通过更好的计算方法来获得
吗?


代码:

int is_leap(int year)

{

开关(年份%19){

案例0:案例3:案例6:案例8:

案例11:案例14:案例17:返回1;

默认:返回0;

}

}

这是日历程序的一部分。

I''m new here, so excuse me if my style is incorrect. Can anyone come
up with a better method for this calculation?

Code:
int is_leap(int year)
{
switch (year % 19) {
case 0: case 3: case 6: case 8:
case 11: case 14: case 17: return 1;
default: return 0;
}
}

This is part of a calendar program.

推荐答案

文章< 11 ******* ***************@o3g2000hsb.googlegroups。 com>,

< ma ****** @ gmail.comwrote:
In article <11**********************@o3g2000hsb.googlegroups. com>,
<ma******@gmail.comwrote:

>我是新来的,如果我的风格不正确,请原谅。任何人都可以使用更好的方法进行此计算吗?
>I''m new here, so excuse me if my style is incorrect. Can anyone come
up with a better method for this calculation?


>代码:
int is_leap(int year)
{

switch(年份) %19){

案例0:案例3:案例6:案例8:

案例11:案例14:案例17:返回1;

默认:返回0;

}
}
>Code:
int is_leap(int year)
{
switch (year % 19) {
case 0: case 3: case 6: case 8:
case 11: case 14: case 17: return 1;
default: return 0;
}
}



1900%19 == 0但1900年不是闰年(特例)

1903%19 == 3但1903年不是闰年。

1906%19 == 6但1906年不是闰年。 />
1911%19 == 11但1911年不是闰年。

1914%19 == 14但1914年不是闰年。

1917%19 == 17但1917年不是闰年。

2000%19 == 5但2000 *是*闰年。

2004%19 == 9但是2004 *是*闰年。


这不是一个简单的例子,必须减去一年的基础年

才能开始周期:

你在周期的第0年有一个闰年,而另一个在b / b
年+3(不是年份+4),另一年是+6,然后是

仅仅2年后的年份+8。显然这是错误的。

-

任何足够先进的错误都与功能无法区分。

- Rich Kulawiec

1900 % 19 == 0 but 1900 was not a leap year (special case)
1903 % 19 == 3 but 1903 was not a leap year.
1906 % 19 == 6 but 1906 was not a leap year.
1911 % 19 == 11 but 1911 was not a leap year.
1914 % 19 == 14 but 1914 was not a leap year.
1917 % 19 == 17 but 1917 was not a leap year.
2000 % 19 == 5 but 2000 *was* a leap year.
2004 % 19 == 9 but 2004 *was* a leap year.

It isn''t a simple case of having to subtract an base year
to get to the start of the cycle:
you have a leap year at year 0 of the cycle, and another at
year +3 (not year +4), another at year +6, then
one just 2 years later at year +8. Clearly this is wrong.
--
"Any sufficiently advanced bug is indistinguishable from a feature."
-- Rich Kulawiec


ma******@gmail.com 说:

我是新来的,如果我的风格不正确,请原谅。任何人都可以通过更好的计算方法来获得
吗?


代码:

int is_leap(int year)

{

开关(年份%19){

案例0:案例3:案例6:案例8:

案例11:案例14:案例17:返回1;

默认:返回0;

}

}

这是日历计划的一部分。
I''m new here, so excuse me if my style is incorrect. Can anyone come
up with a better method for this calculation?

Code:
int is_leap(int year)
{
switch (year % 19) {
case 0: case 3: case 6: case 8:
case 11: case 14: case 17: return 1;
default: return 0;
}
}

This is part of a calendar program.



这是一个更好的方法:


int really_is_leap(int year)

{

返回年份%4 == 0&& (年%100!= 0 ||年%400 == 0);

}


这个具有实际给出正确结果的优点。


测试你的功能,看看它如何在你知道要飞跃的年份工作

年(例如1976年,2000年,2004年,2008年)和年你知道不要闰年

(2001,2002,2003,2005)。


然后切换到工作算法。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Here''s a better method:

int really_is_leap(int year)
{
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}

This one has the merit of actually giving the right results.

Test your function, and see how it works on years that you know to be leap
years (eg 1976, 2000, 2004, 2008) and years you know not to be leap years
(2001, 2002, 2003, 2005).

Then switch to a working algorithm.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


11月5日下午12:30,mazwo ... @ gmail.com写道:
On Nov 5, 12:30 pm, mazwo...@gmail.com wrote:

我是新来的,如果我的风格不正确,请原谅。任何人都可以通过更好的计算方法来获得
吗?


代码:

int is_leap(int year)

{

开关(年份%19){

案例0:案例3:案例6:案例8:

案例11:案例14:案例17:返回1;

默认:返回0;

}


}


这是日历计划的一部分。
I''m new here, so excuse me if my style is incorrect. Can anyone come
up with a better method for this calculation?

Code:
int is_leap(int year)
{
switch (year % 19) {
case 0: case 3: case 6: case 8:
case 11: case 14: case 17: return 1;
default: return 0;
}

}

This is part of a calendar program.



试试这个:


return(年%400 == 0)|| ((年%4 == 0)&&(年%100!= 0));


我认为这是正确的公式(可被400整除)或者均匀地

可被4整除,并且不能被100整除。

Try this:

return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));

I think that''s the correct formula (evenly divisible by 400, or evenly
divisible by 4 and not evenly divisible by 100).


这篇关于计算闰年的更好算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆