zoukankan      html  css  js  c++  java
  • 九度OJ 1043:Day of Week(星期几) (日期计算)

    时间限制:1 秒

    内存限制:32 兆

    特殊判题:

    提交:5349

    解决:1923

    题目描述:

    We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.
    For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
    Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

    输入:

    There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.

    输出:

    Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

    样例输入:
    9 October 2001
    14 October 2001
    
    样例输出:
    Tuesday
    Sunday
    提示:

    Month and Week name in Input/Output:
    January, February, March, April, May, June, July, August, September, October, November, December
    Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

    来源:
    2008年上海交通大学计算机研究生机试真题

    思路:

    日期计算类的题目不少,虽然不难,但容易出错。

    需要注意的地方主要是闰年的计算。

    一般的年是365天,二月是28天,而闰年则366天,2月是29天。

    闰年的划定标准是:400的倍数,或者4的倍数但不是100的倍数。


    代码:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    #define N 10
     
    int compare(int y[2], int m[2], int d[2])
    {
        if (y[0] != y[1])
            return y[0]-y[1];
        else if (m[0] != m[1])
            return m[0]-m[1];
        else if (d[0] != d[1])
            return d[0]-d[1];
        else
            return 0;
    }
     
    void swap(int a[2])
    {
        int tmp;
        tmp = a[0];
        a[0] = a[1];
        a[1] = tmp;
    }
     
    int days(int y, int m, int d)
    {
        int count = 0;
     
        //printf("y=%d, m=%d, d=%d
    ", y, m, d);
     
        count += y*365;
        count += (y-1)/4+1;
        count -= (y-1)/100+1;
        count += (y-1)/400+1;
        //printf("count=%d
    ", count);
     
        if (m > 1)
            count += 31;
        if (m > 2)
        {
            if ((y%4 == 0 && y%100 != 0) || y%400 == 0)
                count += 29;
            else
                count += 28;
        }
        if (m > 3)
            count += 31;
        if (m > 4)
            count += 30;
        if (m > 5)
            count += 31;
        if (m > 6)
            count += 30;
        if (m > 7)
            count += 31;
        if (m > 8)
            count += 31;
        if (m > 9)
            count += 30;
        if (m > 10)
            count += 31;
        if (m > 11)
            count += 30;
        if (m > 12)
            count += 31;
        //printf("count=%d
    ", count);
     
        count += d;
        //printf("count=%d
    ", count);
     
        return count;
    }
     
    int month(char s[])
    {
        char a[12][20] = {"January", "February", "March", "April",
                        "May", "June", "July", "August",
                        "September", "October", "November", "December"};
        int i;
        for (i=0; i<12; i++)
        {
            if (strcmp(s, a[i]) == 0)
                break;
        }   
        return i+1;
    }   
         
    void pweek(int w1)
    {   
        char s[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday",
                        "Thursday", "Friday", "Saturday"};
        printf("%s
    ", s[w1]);
    }   
             
    int main(void)
    {       
        char s[N];
        int y[2], m[2], d[2], w[2];
         
        y[0] = 2001; 
        m[0] = 10;
        d[0] = 9; 
        w[0] = 2;
        while (scanf("%d%s%d", &d[1], s, &y[1]) != EOF)
        {
            m[1] = month(s);
            w[1] = w[0];
            int cmp = compare(y, m, d);
            if (cmp < 0)
            {
                w[1] = (w[0] + days(y[1], m[1], d[1])
                        - days(y[0], m[0], d[0])) % 7;
            }
            else if (cmp > 0)
            {
                w[1] = (w[0] + 7 - (days(y[0], m[0], d[0])
                        - days(y[1], m[1], d[1])) % 7) % 7;
            }           
            pweek(w[1]);
        }
         
        return 0;
    }       
    /**************************************************************
        Problem: 1043
        User: liangrx06
        Language: C
        Result: Accepted
        Time:0 ms
        Memory:916 kb
    ****************************************************************/


    编程算法爱好者。
  • 相关阅读:
    网站访问量和服务器带宽的选择
    PHP实现四种基本排序算法
    常用的PHP排序算法以及应用场景
    常见的mysql数据库sql语句的编写和运行结果
    MyBatis拦截器:给参数对象属性赋值
    《自律让你自由》摘要
    Java JDK1.5、1.6、1.7新特性整理(转)
    人人都能做产品经理吗?
    Windows下查询进程、端口
    一语收录(2016-09-18)
  • 原文地址:https://www.cnblogs.com/liangrx06/p/5083988.html
Copyright © 2011-2022 走看看