zoukankan      html  css  js  c++  java
  • (Problem 19)Counting Sundays

    You are given the following information, but you may prefer to do some research for yourself.

    • 1 Jan 1900 was a Monday.
    • Thirty days has September,
      April, June and November.
      All the rest have thirty-one,
      Saving February alone,
      Which has twenty-eight, rain or shine.
      And on leap years, twenty-nine.
    • A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

    How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

    #include <stdio.h> 
    #include <stdbool.h>
    
    const int a[2][12] = {{31,28,31,30,31,30,31,31,30,31,30,31},
                         {31,29,31,30,31,30,31,31,30,31,30,31}};
    
    
    bool leapYear(int n)  //判断闰年
    {
        return (((n % 4 ==0) && (n % 100 !=0)) || (n % 400 == 0));
    }
    
    bool issunday(int n) //判断某天是否是星期天
    {
        return (n % 7 == 0 ? true : false);
    }
    
    void solve(void)
    {
        int num, i, j, count;
        count = 0;
    
        i = 1901;
        num = 1;
        while(i < 2000) {
    
            int t = (leapYear(i) ? 1 : 0);   //判断闰年
            for(j = 0; j < 12; j++) {
                num += a[t][j];
                if(issunday(num)) count++;
            }
            i++;
        }
        printf("%d
    ",count);
    }
    
    int main(void)
    {
        solve();
        return 0;
    }
    Answer:
    171
    作者:acutus
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    [原]Android 开发第一步
    [转]使用Android-Studio 开发Android 程序
    [转]VS2010 常用插件
    [转]FluentData
    BUUCTF-[HCTF 2018]WarmUp
    2019.11.11读书笔记
    2019.11.9读书笔记
    记录一道神仙CTF-wtf.sh-150
    SDOI2018 一轮培训划水祭
    [SHOI2009]会场预约
  • 原文地址:https://www.cnblogs.com/acutus/p/3514075.html
Copyright © 2011-2022 走看看