zoukankan      html  css  js  c++  java
  • 2008上交:Day of Week

    题目描述:

    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

    思路:先2016年7月3日(正好为星期日)为基准。求两日期之间的差值时可分别求两年份与1000年1月1日之间的距离,再做差。
    #include <iostream>
    #include <string.h>
    using namespace std;
    char Month[13][20]={"Null","January","February","March","April","May","June","July","August","September","October","November","December"};
    char Week[8][20]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    int mds[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int d,y,m;
    char mon[20];
    int sMon(char s[])
    {
        for(int i=1;i<=12;i++)
        {
            if(strcmp(Month[i],s)==0)
            {
                return i;
            }
        }
    }
    bool isLeap(int year)
    {
        if((year%4==0&&year%100!=0)||year%400==0)
        {
            return true;
        }
        return false;
    }
    int caldays(int year)
    {
        int sum=0;
        for(int i=1000;i<=year;i++)
        {
            if(isLeap(i))
            {
                sum+=366;
            }
            else    sum+=365;
        }
        return sum;
    }
    int standard;
    int main()
    {
        standard+=caldays(2015);
        for(int i=1;i<=6;i++)
        {
            standard+=mds[i];
        }
        if(isLeap(2016))    standard+=1;
        standard+=3;
        while(cin>>d>>mon>>y)
        {
            m=sMon(mon);
            int days=0;
            days+=caldays(y-1);//本年不算 
            for(int i=1;i<m;i++)//本月不算 
            {
                days+=mds[i];
            }
            if(m>2&&isLeap(y))    days+=1;
            days+=d;
            int diff=days-standard;
            if(diff>=0)
            {
                diff%=7;
                cout<<Week[diff]<<endl;
            }
            else
            {
                diff=-diff;
                diff%=7;
                if(diff!=0)    diff=7-diff;
                cout<<Week[diff]<<endl;
            }
        }    
        return 0;
    }
  • 相关阅读:
    浅析C# new 和Override的区别
    用流打开open office ods 文件
    两个自己写的合并GridView 行的方法
    TSQL 日期格式化
    页面刷新后滚动条定位
    解决 TextBox 的 ReadOnly 属性为 true 时,刷新页面后值丢失的方法
    Sql server 查询条件中将通配符作为文字使用
    window.open 弹出页面回写父页面值及触发父页面Button事件
    注册光标丢失的事件
    模态对话框对父页面控件回写值
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5641745.html
Copyright © 2011-2022 走看看