Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

容易理解的思路

Posted by makuiyu at 2014-01-11 10:51:07 on Problem 2080
按天计算超时,索性就先按年计算,然后剩下的按天计算

char week[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday",
                    "Thursday", "Friday", "Saturday"};
int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool is_leap_year(int y)
{
    return (y%400==0 || (y%100!=0 && y%4==0));
}

int main()
{
    int n, y, m, d, w;
    while(scanf("%d", &n)!=EOF && n!=-1)
    {
        w = (6+n) % 7;//计算星期
        for(y = 2000, m = 1, d = 1; n>365; n-=365,++y)
        //按年计算,每次减365,如果是闰年,再减1
            if(is_leap_year(y))
                --n;
        while(n--)//按天计算
        {
            if(is_leap_year(y))
                month[2] = 29;
            else
                month[2] = 28;
            ++d;
            if(d > month[m])
            {
                d = 1;
                ++m;
                if(m > 12)
                {
                    m = 1;
                    ++y;
                }
            }
        }
        printf("%04d-%02d-%02d %s\n", y, m, d, week[w]);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator