zoukankan      html  css  js  c++  java
  • 日起计算2

    //hdu 2133 what day is it
    //题意:给一个日期计算是周几
    //再次我采用从该日期到1年1月1日有多天,这时候y年以前比较好算,即(y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400,(x/4-x/100+x/400),这样是直接计算出日期,
    要是你选择跑循环计算也行,还有就是记住一些常识1年1月1日 是周一, 2000年1月1日 是周六, 0年1月1日是周六

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    
    #define N 100;
    
    int tab[2][13]= {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                     {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
    
    char s[7][20]={{"Monday"}, {"Tuesday"},{"Wednesday"}, {"Thursday"},
     {"Friday"}, {"Saturday"}, {"Sunday"}};
    
    int IsLeap(int n)
    {
        return ((n%4==0&&n%100!=0) || n%400==0);
    }
    
    
    int main()
    {
        int y, m, d, leap, t;
    
        while(scanf("%d%d%d", &y, &m, &d)!=EOF)
        {
            leap=IsLeap(y);
            if(m*d==0 || d>tab[leap][m])
            {
                printf("illegal
    ");
                continue ;
            }
            t=0;
            t+=(y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400;
            for(int i=1; i<m; i++)
                t+=tab[leap][i];
            t+=d;
            t%=7;
            if(t==0)
                t=7;
            printf("%s
    ", s[t-1]);
        }
        return 0;
    }
  • 相关阅读:
    java基础--日期--练习集锦
    java基础-数字与字符串-练习集锦
    java-多态-之疑惑
    java-接口与多态-
    java基础 -继承-练习集锦
    java基础-对象-练习集锦
    java基础--数组--练习集锦
    hibernate第二天
    jquery的一些笔记
    struts入门2
  • 原文地址:https://www.cnblogs.com/9968jie/p/5419344.html
Copyright © 2011-2022 走看看