首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

求皇历算法,不要源码

2012-08-15 
求万年历算法,不要源码能判断该日是星期几,此年闰年还是平年[解决办法]上面所有的代码,求闰年的部分都是错

求万年历算法,不要源码
能判断该日是星期几,此年闰年还是平年

[解决办法]
上面所有的代码,求闰年的部分都是错误的!你们的程序当年份小于3200时是正确的,再往后就都错了。比如3200年实际上不是闰年,但你们的程序却显示是闰年。实际上真正求闰年的算法还是有点难度的,要从本质上理解才能实现。
在计算闰年时,你们只是把年份对4、100和400求模,根本不知道为什么是4、100、400这三个数,后面还有没有其他的数。其实4、100和400这三个数是人为定的,只是选这几个数更容易计算,当然也可以是其他的数,比如4、120......。而在400的后面还有其他的数。
我的程序只需要你指定前面的几个数(当然这个数要合理),然后会以最优的方法自动算出后面的所有求模的数,最后给出闰年的判断。

下面是我的程序。

C/C++ code
#include <iostream>using namespace std;int leap_year_array(unsigned int *a, int n)//计算闰年的整除数组,要求a[0]必须为1{    const unsigned int one_day = 24*3600;        int bias = 5*3600 + 48*60 + 46;    for (int i=1; i<n; ++i)    {        if (a[i]==0)//若a[i]!=0则说明此项为用户设定的值        {                        a[i] = one_day/bias*a[i-1];        }            bias = one_day - a[i]/a[i-1]*bias;        if (bias < 0)        {            cout<<"the setting number is wrong!"<<endl;            return i;        }        if (bias == 0)        {            return i+1;        }            }    return n;}bool check_leap_year(unsigned int *a, int n, unsigned int year){    for (int i=1; i<n && !(year%a[i]); ++i);    if (i%2==1)    {        return false;    }    else    {        return true;    }}bool is_leap_year(unsigned int *a, int a_len, unsigned int year){    int array_len = leap_year_array(a, a_len);    if (array_len >= a_len)    {        cout<<"the array's size is not large enough!"<<endl;        return false;    }    else    {        if (check_leap_year(a, array_len, year))        {            cout<<"is leap year!"<<endl;            return true;        }        else        {            cout<<"is not leap year!"<<endl;            return false;        }    }}int main(){    unsigned int year = 3200;    const int len = 10;    unsigned int a[len] = {1,4,100,0,0,0,0,0,0,0};//此数组的第一位必须为1,其他位用户可以设定期望值,如果用户希望由电脑计算,可设为零。    is_leap_year(a, len, year);    return 0;} 

热点排行