鱼C论坛

 找回密码
 立即注册
查看: 4243|回复: 22

题目19:20世纪有多少个星期日是当月的第一天?

[复制链接]
发表于 2015-4-21 17:01:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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)?

题目:

以下是一些已知信息,但是或许你需要自己做一些其他的调查。

  • 1900 年 1 月 1 日是星期一。
  • 30 天的月份有:9 月,4 月,6 月,1 1月。
  • 此外的月份都是 31 天,当然 2 月除外。
  • 2 月在闰年有 29 天,其他时候有 28 天。
  • 年份可以被 4 整除的时候是闰年,但是不能 400 整除的世纪年(100 的整数倍年)除外。

20 世纪(1901 年 1 月 1 日到 2000 年 12 月 31 日)一共有多少个星期日落在了当月的第一天?

评分

参与人数 1荣誉 +1 收起 理由
cwhsmile + 1 171,这题简单,

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-7-1 13:34:23 | 显示全部楼层
目前电脑只有数据库环境,先用sql写一下吧 . 其实结果不是最重要的,思路有了, 不管什么语言,就都能实现了.
set serveroutput on;
DECLARE
v_month INTEGER;
v_start date;
v_end   date;
v_3 INTEGER;
v_enddays date;
j INTEGER := 0;
begin
  v_start := date'1900-01-01';
  v_end   := date'2001-01-01';
  v_month := months_between(v_end,v_start);
  for v_3 in 0..v_month-1
  LOOP
        v_enddays := add_months(v_start,v_3);
        if TRIM(to_char(v_enddays,'day'))= 'sunday' then
                j:=j+1;
                -- dbms_output.put_line(v_enddays);
        end if;
  end loop;
    dbms_output.put_line(j);
end;
/

ps : 原来oracle检索出来的结果带空格,导致我的计算结果一直为0 --!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 2 反对 1

使用道具 举报

发表于 2016-8-27 15:59:11 | 显示全部楼层
  1. y = 1900
  2. m = 1
  3. xingqi = 1
  4. day = 1
  5. count = 0
  6. def run(x):
  7.     if x%400 == 0:
  8.         return True
  9.     else:
  10.         if x%100 == 0:
  11.             return False
  12.         if x%4 == 0:
  13.             return True
  14.         return False




  15. while y < 2001:

  16.     if m in [4,6,9,11]:
  17.         if day == 31:
  18.             m += 1
  19.             day = 1
  20.             
  21.     if m in [1,3,5,7,8,10,12]:
  22.         if day == 32:
  23.             m += 1
  24.             day = 1

  25.     if m == 13:
  26.         y += 1
  27.         m = 1

  28.     if m == 2:
  29.         if run(y) == True:
  30.             if day == 30:
  31.                 m += 1
  32.                 day = 1
  33.         else:
  34.             if day == 29:
  35.                 m += 1
  36.                 day = 1

  37.     if xingqi == 8:
  38.         xingqi = 1
  39.         
  40.     if y > 1900 and day == 1 and xingqi == 7:
  41.         count += 1      
  42.     day += 1
  43.     xingqi += 1
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-11 15:48:23 | 显示全部楼层
本帖最后由 jerryxjr1220 于 2017-10-4 14:20 编辑

重新查证了一下,题目错了,1901年1月1日是星期二。
171
[Finished in 0.1s]

直接用datetime库
  1. import datetime as dt
  2. c = 0
  3. for y in range(1901,2001):
  4.         for m in range(1,13):
  5.                 dd = dt.datetime(y,m,1)
  6.                 if dd.weekday()==6:
  7.                         c += 1
  8. print(c)
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-10 22:08:37 | 显示全部楼层
  1. # encoding:utf-8
  2. # 20世纪每月的第一天是星期天的  有多少

  3. '''
  4. 蔡勒(Zeller)公式W=Y+[Y/4]+[C/4]-2C+[26(M+1)/10]+d-1
  5. 公式中的符号含义如下:W为星期数;C为世纪;Y为年(两位数);
  6. M为月数(M=m(当m>2);M=m+12(m<3));d为日。
  7. 相比于通用通用计算公式而言,蔡勒(Zeller)公式大大降低了计算的复杂度。
  8. '''
  9. from time import time

  10. def euler019():
  11.     count = 0
  12.     for year in range(1901, 2001):
  13.         for month in range(1, 13):
  14.             temp = str(year) + '-' + str(month) + '-' + '1'
  15.             # 1、2月份,看做上一年的13、14月份
  16.             if month in [1, 2]:
  17.                 M = month + 12
  18.                 Y = year - 1
  19.             else:
  20.                 M = month
  21.                 Y = year
  22.             # 世纪数  年份的前两位
  23.             C = int(year / 100)
  24.             # 年  年份的后两位
  25.             Y = Y % 100
  26.             # 日  日期数 本题默认为1
  27.             D = 1
  28.             W = (Y + int(Y / 4) + int(C / 4) - 2 * C + int(26 * (M + 1) / 10) + D - 1) % 7
  29.             if not W:
  30.                 count += 1
  31.                 #print(temp)
  32.     return count

  33. if __name__ == '__main__':
  34.     start = time()
  35.     print(euler019())
  36.     print('cost %.6f sec' % (time() - start))
复制代码


171
cost 0.003001 sec
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-18 16:44:49 | 显示全部楼层
此代码使用matlab编程
Problem19所用时间为0.0055557秒
Problem19的答案为171
  1. %题目19:20世纪有多少个星期日是当月的第一天?
  2. function Output=Problem19(Input)
  3. tic
  4. if nargin==0
  5.     Input=7;%星期天
  6. end
  7. Common=[1:31,1:28,1:31,1:30,1:31,1:30,1:31,1:31,1:30,1:31,1:30,1:31];%平年序列
  8. Leap=[1:31,1:29,1:31,1:30,1:31,1:30,1:31,1:31,1:30,1:31,1:30,1:31];%闰年序列
  9. Century=[];%一个世纪的时间序列
  10. Sum=0;%当月第一天为星期天的总天数
  11. for ii=1901:2000
  12.     if mod(ii,4)==0
  13.         Temp=[Century,Leap];
  14.     else
  15.         Temp=[Century,Common];
  16.     end
  17.     Century=Temp;
  18. end
  19. L=length(Century);%世纪的长度
  20. Week=1:7;
  21. if mod(L,7)==0
  22.     Weekday=repmat(Week,[1 L/7]);%星期时间序列
  23. else
  24.     Weekday=repmat(Week,[1 (L+7-mod(L,7))/7]);
  25. end
  26. Weekday=Weekday(2:end);%1901年1月1日是星期2
  27. for jj=1:L
  28.     if Weekday(jj)==Input&&Century(jj)==1%当天为星期天的同时,也是当月的第一天
  29.         Sum=Sum+1;
  30.     end
  31. end
  32. toc
  33. Output=Sum;
  34. disp('此代码使用matlab编程')
  35. disp(['Problem19所用时间为',num2str(toc),'秒'])
  36. disp(['Problem19的答案为',num2str(Output)])
  37. end
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-2-22 08:26:37 | 显示全部楼层
  1. number = 0
  2. day = 1
  3. days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

  4. def isleap(_year):
  5.     if (_year % 4 == 0 and _year % 100 != 0) or (_year % 400 == 0):
  6.         return True
  7.     else:
  8.         return False

  9. for year in range(1900,2001):
  10.     if isleap(year):
  11.         days[1] = 29
  12.     else:
  13.         days[1] = 28
  14.     for i in days:
  15.         day += i
  16.         if day % 7 == 0 and year >= 1901:
  17.             number += 1

  18. print number
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-2-22 10:03:09 | 显示全部楼层
jerryxjr1220 发表于 2016-10-11 15:48
重新查证了一下,题目错了,1901年1月1日是星期二。
171
[Finished in 0.1s]

看清楚题目。。题目说的是1900年1月1日是星期一。。。
然后问题说的是从1901年1月1日开始算起。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-7 13:42:07 | 显示全部楼层
171

  1. month=[31,28,31,30,31,30,31,31,30,31,30,31]
  2. sunday=0
  3. xingqi=1
  4. xingqi=(xingqi+365%7)%7
  5. for years in range (1901,2001):
  6.     if years%4==0:
  7.         month[1]=29
  8.     month[1]=28

  9.     for i in month:
  10.         xingqi=(xingqi+i%7)%7
  11.         if xingqi==0:
  12.             sunday+=1
  13. print(sunday)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-15 11:00:28 | 显示全部楼层
本帖最后由 99592938 于 2017-3-15 11:05 编辑
  1. wek = (1+365)%7 #1901-01-01是星期几
  2. list_sun=[]
  3. mon=[31,28,31,30,31,30,31,31,30,31,30,31]
  4. for year in range(1901,2001):
  5.     if year%4:mon[1]=28
  6.     else:mon[1]=29
  7.     for m in range(12):
  8.         if wek==0:list_sun.append('%d-%d' %(year,m+1))
  9.         wek=(wek+mon[m])%7
  10. print(len(list_sun))
  11. print(list_sun)
复制代码


结果:
171
['1901-9', '1901-12', '1902-6', '1903-2', '1903-3', '1903-11', '1904-5', '1905-1', '1905-10', '1906-4', '1906-7', '1907-9', '1907-12', '1908-3', '1908-11', '1909-8', '1910-5', '1911-1', '1911-10', '1912-9', '1912-12', '1913-6', '1914-2', '1914-3', '1914-11', '1915-8', '1916-10', '1917-4', '1917-7', '1918-9', '1918-12', '1919-6', '1920-2', '1920-8', '1921-5', '1922-1', '1922-10', '1923-4', '1923-7', '1924-6', '1925-2', '1925-3', '1925-11', '1926-8', '1927-5', '1928-1', '1928-4', '1928-7', '1929-9', '1929-12', '1930-6', '1931-2', '1931-3', '1931-11', '1932-5', '1933-1', '1933-10', '1934-4', '1934-7', '1935-9', '1935-12', '1936-3', '1936-11', '1937-8', '1938-5', '1939-1', '1939-10', '1940-9', '1940-12', '1941-6', '1942-2', '1942-3', '1942-11', '1943-8', '1944-10', '1945-4', '1945-7', '1946-9', '1946-12', '1947-6', '1948-2', '1948-8', '1949-5', '1950-1', '1950-10', '1951-4', '1951-7', '1952-6', '1953-2', '1953-3', '1953-11', '1954-8', '1955-5', '1956-1', '1956-4', '1956-7', '1957-9', '1957-12', '1958-6', '1959-2', '1959-3', '1959-11', '1960-5', '1961-1', '1961-10', '1962-4', '1962-7', '1963-9', '1963-12', '1964-3', '1964-11', '1965-8', '1966-5', '1967-1', '1967-10', '1968-9', '1968-12', '1969-6', '1970-2', '1970-3', '1970-11', '1971-8', '1972-10', '1973-4', '1973-7', '1974-9', '1974-12', '1975-6', '1976-2', '1976-8', '1977-5', '1978-1', '1978-10', '1979-4', '1979-7', '1980-6', '1981-2', '1981-3', '1981-11', '1982-8', '1983-5', '1984-1', '1984-4', '1984-7', '1985-9', '1985-12', '1986-6', '1987-2', '1987-3', '1987-11', '1988-5', '1989-1', '1989-10', '1990-4', '1990-7', '1991-9', '1991-12', '1992-3', '1992-11', '1993-8', '1994-5', '1995-1', '1995-10', '1996-9', '1996-12', '1997-6', '1998-2', '1998-3', '1998-11', '1999-8', '2000-10']
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-15 22:29:50 | 显示全部楼层
#include<stdio.h>
int leapyear(int year)
{
        if((0==year%4&&0!=year%100)||0==year%400)
        {
                return 1;
        }
        else
        {
                return 0;
        }
}

int month(int y,int m)
{
        switch(m)
        {
                case 1:
                        return 31;
                case 2:
                        if(leapyear(y))
                        {
                                return 29;
                        }
                        else
                        {
                                return 28;
                        }
                case 3:
                        return 31;
                case 4:
                        return 30;
                case 5:
                        return 31;
                case 6:
                        return 30;
                case 7:
                        return 31;
                case 8:
                        return 31;
                case 9:
                        return 30;
                case 10:
                        return 31;
                case 11:
                        return 30;
                default:
                        return 31;
        }

}

int main()
{
        int i,y,m,d,sum=0,t=1;
        for(y=1900;y<2001;y++)
        {
                for(m=1;m<=12;m++)
                {
                        d=1;
                        while(d<=month(y,m))
                        {
                                for(i=t;i<=7;i++)
                                {
                                        if(7==i&&1==d)
                                        {
                                                sum=sum+1;
                                        }
                                        d++;                               
                                        if(d>month(y,m))
                                        {
                                                if(i<7)
                                                {
                                                        t=i+1;
                                                }
                                                else
                                                {
                                                        t=1;
                                                }
                                                break;
                                        }
                                        t=1;
                                }
                        }
                }
        }
        printf("%d\n",sum-2);//减去1990年的两个
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-28 20:49:33 | 显示全部楼层
  1. #include <iostream>

  2. using namespace std;

  3. int main()
  4. {
  5.         const short NYEAR[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  6.         const short RYEAR[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
  7.         int day=6,month=1,sum=0;
  8.         for(int year=1901;year<=2000;++year)
  9.         {
  10.                 if(year%4==0&&year%100!=0||year%400==0)
  11.                 {
  12.                         while(month<=12)
  13.                         {
  14.                                 day+=7;
  15.                                 if(day>=RYEAR[month])
  16.                                 {
  17.                                         day-=RYEAR[month];
  18.                                         ++month;
  19.                                         if(day==1)
  20.                                         {
  21.                                                 ++sum;
  22.                                         }
  23.                                 }
  24.                         }
  25.                         month=1;
  26.                 }
  27.                 else
  28.                 {
  29.                         while(month<=12)
  30.                         {
  31.                                 day+=7;
  32.                                 if(day>=NYEAR[month])
  33.                                 {
  34.                                         day-=NYEAR[month];
  35.                                         ++month;
  36.                                         if(day==1)
  37.                                         {
  38.                                                 ++sum;
  39.                                         }
  40.                                 }
  41.                         }
  42.                         month=1;
  43.                 }
  44.         }
  45.         cout<<"总共有"<<sum<<"个星期日是每个月的第一天。";
  46. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-15 20:38:41 | 显示全部楼层
python3 时间为0.001526 s
结果是171
  1. import time

  2. start = time.clock()
  3. yearn = {1: 3, 2: 0, 3: 3, 4: 2, 5: 3, 6: 2,
  4.          7: 3, 8: 3, 9: 2, 10: 3, 11: 2, 12: 3}
  5. yearr = {1: 3, 2: 1, 3: 3, 4: 2, 5: 3, 6: 2,
  6.          7: 3, 8: 3, 9: 2, 10: 3, 11: 2, 12: 3}

  7. count = 0
  8. daysum = 2
  9. n = 13
  10. for y in range(1901, 2001):
  11.     if ((y % 4 == 0 and y % 100 != 0) or y % 400 == 0):
  12.         year = yearr
  13.     else:
  14.         year = yearn
  15.     if y == 2000:
  16.         n = 12
  17.     for m in range(1, n):
  18.         daysum = (daysum + year[m]) % 7
  19.         if daysum == 0:
  20.             if m + 1 == 13:
  21.                 print("%d-%d-1" % (y + 1, 1))
  22.             else:
  23.                 print("%d-%d-1" % (y, m + 1))
  24.             count += 1
  25. print("共有%d个" % count)

  26. end = time.clock()
  27. print("read:%f s" % (end - start))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-26 14:55:13 | 显示全部楼层
答案是171
  1. mport time
  2. import math

  3. def test1():
  4.     mon = {1:31,2:'leap year',3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
  5.     wed = 1
  6.    
  7.     for m in range(1900,2001):
  8.         if (m%4==0 and m%100!=0) or (m%400==0):
  9.             leap = 1
  10.         else:
  11.             leap = 0
  12.         for n in range(1,13):
  13.             if n == 2:
  14.                 if leap == 1:
  15.                     mon[2] = 29
  16.                 else:
  17.                     mon[2] = 28
  18.             for i in range(1,mon[n]+1):
  19.                 if wed == 8:
  20.                     wed = 1
  21.                 if i == 1 and wed == 7:
  22.                     if m != 1900:
  23.                         li.append([m,n,i])
  24.                 wed += 1

  25.    
  26.    
  27.     return li,len(li)
  28. li = []
  29. start = time.perf_counter()
  30. print(test1())
  31. end = time.perf_counter()
  32. print(end-start)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-6 09:28:01 | 显示全部楼层
jerryxjr1220 发表于 2016-10-11 15:48
重新查证了一下,题目错了,1901年1月1日是星期二。
171
[Finished in 0.1s]

题目说的是1990年1月1日是星期一
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-1-10 13:06:35 | 显示全部楼层
本帖最后由 guoquanli 于 2020-1-10 13:13 编辑
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdbool.h>
  4. //是否是润年i的判断
  5. bool IsLeapYear(int year){
  6.     if((year%4 == 0 && year%100 != 0) || year %400 == 0){
  7.         return true;
  8.     }else{
  9.         return false;
  10.     }
  11. }
  12. //测试函数
  13. void testIsLeapYear(int year){
  14.     if(IsLeapYear(year)){
  15.         printf("%d year is leap year!\n",year);
  16.     }else{
  17.         
  18.         printf("%d year is not leap year!\n",year);
  19.     }
  20. }
  21. // 判断从1900年到year年总共有多少天
  22. int DayCal(int year){
  23.         int sum = 0;
  24.         for(int i = 1900; i < year; i++){
  25.                 if(IsLeapYear(i)){
  26.                         sum += 366;
  27.                 }else{
  28.                         sum += 365;
  29.                 }
  30.         }
  31.         return sum;
  32. }       
  33. //测试函数
  34. void testDayCal(int year){
  35.         printf("from 1900 to this year,toal day :%d\n",DayCal(year));
  36. }

  37. //统计输入的年份中,当月第一天是星期日
  38. void FindMonth(int year){
  39.         int sum  = DayCal(year);
  40.         bool flag = IsLeapYear(year);
  41.         int dayArray[12] = {0,31,28,31,30,31,30,31,31,30,31,30};
  42.         if(flag){
  43.                 dayArray[2] = 29;
  44.         }
  45.         for(int i = 0; i<12;i++){
  46.                 sum += dayArray[i];
  47.                 if((sum  + 1  ) % 7 == 0){
  48.                         printf("%d year %d month first day is Sunday!\n",year,i+1);
  49.                 }       
  50.         }
  51. }
  52. void main(){
  53.         for(int i = 1900; i<2000;i++){
  54.                 printf("*************%d year search result *********\n",i);
  55.                 FindMonth(i);       
  56.                 printf("\n");
  57.                        
  58.         }
  59. }
  60. ======运行时间======
  61. real        0m0.005s
  62. user        0m0.000s
  63. sys        0m0.005s

  64. ======运行结果======
  65. *************1900 year search result *********
  66. 1900 year 4 month first day is Sunday!
  67. 1900 year 7 month first day is Sunday!

  68. *************1901 year search result *********
  69. 1901 year 9 month first day is Sunday!
  70. 1901 year 12 month first day is Sunday!

  71. *************1902 year search result *********
  72. 1902 year 6 month first day is Sunday!

  73. *************1903 year search result *********
  74. 1903 year 2 month first day is Sunday!
  75. 1903 year 3 month first day is Sunday!
  76. 1903 year 11 month first day is Sunday!

  77. *************1904 year search result *********
  78. 1904 year 5 month first day is Sunday!

  79. *************1905 year search result *********
  80. 1905 year 1 month first day is Sunday!
  81. 1905 year 10 month first day is Sunday!

  82. *************1906 year search result *********
  83. 1906 year 4 month first day is Sunday!
  84. 1906 year 7 month first day is Sunday!

  85. *************1907 year search result *********
  86. 1907 year 9 month first day is Sunday!
  87. 1907 year 12 month first day is Sunday!

  88. *************1908 year search result *********
  89. 1908 year 3 month first day is Sunday!
  90. 1908 year 11 month first day is Sunday!

  91. *************1909 year search result *********
  92. 1909 year 8 month first day is Sunday!

  93. *************1910 year search result *********
  94. 1910 year 5 month first day is Sunday!

  95. *************1911 year search result *********
  96. 1911 year 1 month first day is Sunday!
  97. 1911 year 10 month first day is Sunday!

  98. *************1912 year search result *********
  99. 1912 year 9 month first day is Sunday!
  100. 1912 year 12 month first day is Sunday!

  101. *************1913 year search result *********
  102. 1913 year 6 month first day is Sunday!

  103. *************1914 year search result *********
  104. 1914 year 2 month first day is Sunday!
  105. 1914 year 3 month first day is Sunday!
  106. 1914 year 11 month first day is Sunday!

  107. *************1915 year search result *********
  108. 1915 year 8 month first day is Sunday!

  109. *************1916 year search result *********
  110. 1916 year 10 month first day is Sunday!

  111. *************1917 year search result *********
  112. 1917 year 4 month first day is Sunday!
  113. 1917 year 7 month first day is Sunday!

  114. *************1918 year search result *********
  115. 1918 year 9 month first day is Sunday!
  116. 1918 year 12 month first day is Sunday!

  117. *************1919 year search result *********
  118. 1919 year 6 month first day is Sunday!

  119. *************1920 year search result *********
  120. 1920 year 2 month first day is Sunday!
  121. 1920 year 8 month first day is Sunday!

  122. *************1921 year search result *********
  123. 1921 year 5 month first day is Sunday!

  124. *************1922 year search result *********
  125. 1922 year 1 month first day is Sunday!
  126. 1922 year 10 month first day is Sunday!

  127. *************1923 year search result *********
  128. 1923 year 4 month first day is Sunday!
  129. 1923 year 7 month first day is Sunday!

  130. *************1924 year search result *********
  131. 1924 year 6 month first day is Sunday!

  132. *************1925 year search result *********
  133. 1925 year 2 month first day is Sunday!
  134. 1925 year 3 month first day is Sunday!
  135. 1925 year 11 month first day is Sunday!

  136. *************1926 year search result *********
  137. 1926 year 8 month first day is Sunday!

  138. *************1927 year search result *********
  139. 1927 year 5 month first day is Sunday!

  140. *************1928 year search result *********
  141. 1928 year 1 month first day is Sunday!
  142. 1928 year 4 month first day is Sunday!
  143. 1928 year 7 month first day is Sunday!

  144. *************1929 year search result *********
  145. 1929 year 9 month first day is Sunday!
  146. 1929 year 12 month first day is Sunday!

  147. *************1930 year search result *********
  148. 1930 year 6 month first day is Sunday!

  149. *************1931 year search result *********
  150. 1931 year 2 month first day is Sunday!
  151. 1931 year 3 month first day is Sunday!
  152. 1931 year 11 month first day is Sunday!

  153. *************1932 year search result *********
  154. 1932 year 5 month first day is Sunday!

  155. *************1933 year search result *********
  156. 1933 year 1 month first day is Sunday!
  157. 1933 year 10 month first day is Sunday!

  158. *************1934 year search result *********
  159. 1934 year 4 month first day is Sunday!
  160. 1934 year 7 month first day is Sunday!

  161. *************1935 year search result *********
  162. 1935 year 9 month first day is Sunday!
  163. 1935 year 12 month first day is Sunday!

  164. *************1936 year search result *********
  165. 1936 year 3 month first day is Sunday!
  166. 1936 year 11 month first day is Sunday!

  167. *************1937 year search result *********
  168. 1937 year 8 month first day is Sunday!

  169. *************1938 year search result *********
  170. 1938 year 5 month first day is Sunday!

  171. *************1939 year search result *********
  172. 1939 year 1 month first day is Sunday!
  173. 1939 year 10 month first day is Sunday!

  174. *************1940 year search result *********
  175. 1940 year 9 month first day is Sunday!
  176. 1940 year 12 month first day is Sunday!

  177. *************1941 year search result *********
  178. 1941 year 6 month first day is Sunday!

  179. *************1942 year search result *********
  180. 1942 year 2 month first day is Sunday!
  181. 1942 year 3 month first day is Sunday!
  182. 1942 year 11 month first day is Sunday!

  183. *************1943 year search result *********
  184. 1943 year 8 month first day is Sunday!

  185. *************1944 year search result *********
  186. 1944 year 10 month first day is Sunday!

  187. *************1945 year search result *********
  188. 1945 year 4 month first day is Sunday!
  189. 1945 year 7 month first day is Sunday!

  190. *************1946 year search result *********
  191. 1946 year 9 month first day is Sunday!
  192. 1946 year 12 month first day is Sunday!

  193. *************1947 year search result *********
  194. 1947 year 6 month first day is Sunday!

  195. *************1948 year search result *********
  196. 1948 year 2 month first day is Sunday!
  197. 1948 year 8 month first day is Sunday!

  198. *************1949 year search result *********
  199. 1949 year 5 month first day is Sunday!

  200. *************1950 year search result *********
  201. 1950 year 1 month first day is Sunday!
  202. 1950 year 10 month first day is Sunday!

  203. *************1951 year search result *********
  204. 1951 year 4 month first day is Sunday!
  205. 1951 year 7 month first day is Sunday!

  206. *************1952 year search result *********
  207. 1952 year 6 month first day is Sunday!

  208. *************1953 year search result *********
  209. 1953 year 2 month first day is Sunday!
  210. 1953 year 3 month first day is Sunday!
  211. 1953 year 11 month first day is Sunday!

  212. *************1954 year search result *********
  213. 1954 year 8 month first day is Sunday!

  214. *************1955 year search result *********
  215. 1955 year 5 month first day is Sunday!

  216. *************1956 year search result *********
  217. 1956 year 1 month first day is Sunday!
  218. 1956 year 4 month first day is Sunday!
  219. 1956 year 7 month first day is Sunday!

  220. *************1957 year search result *********
  221. 1957 year 9 month first day is Sunday!
  222. 1957 year 12 month first day is Sunday!

  223. *************1958 year search result *********
  224. 1958 year 6 month first day is Sunday!

  225. *************1959 year search result *********
  226. 1959 year 2 month first day is Sunday!
  227. 1959 year 3 month first day is Sunday!
  228. 1959 year 11 month first day is Sunday!

  229. *************1960 year search result *********
  230. 1960 year 5 month first day is Sunday!

  231. *************1961 year search result *********
  232. 1961 year 1 month first day is Sunday!
  233. 1961 year 10 month first day is Sunday!

  234. *************1962 year search result *********
  235. 1962 year 4 month first day is Sunday!
  236. 1962 year 7 month first day is Sunday!

  237. *************1963 year search result *********
  238. 1963 year 9 month first day is Sunday!
  239. 1963 year 12 month first day is Sunday!

  240. *************1964 year search result *********
  241. 1964 year 3 month first day is Sunday!
  242. 1964 year 11 month first day is Sunday!

  243. *************1965 year search result *********
  244. 1965 year 8 month first day is Sunday!

  245. *************1966 year search result *********
  246. 1966 year 5 month first day is Sunday!

  247. *************1967 year search result *********
  248. 1967 year 1 month first day is Sunday!
  249. 1967 year 10 month first day is Sunday!

  250. *************1968 year search result *********
  251. 1968 year 9 month first day is Sunday!
  252. 1968 year 12 month first day is Sunday!

  253. *************1969 year search result *********
  254. 1969 year 6 month first day is Sunday!

  255. *************1970 year search result *********
  256. 1970 year 2 month first day is Sunday!
  257. 1970 year 3 month first day is Sunday!
  258. 1970 year 11 month first day is Sunday!

  259. *************1971 year search result *********
  260. 1971 year 8 month first day is Sunday!

  261. *************1972 year search result *********
  262. 1972 year 10 month first day is Sunday!

  263. *************1973 year search result *********
  264. 1973 year 4 month first day is Sunday!
  265. 1973 year 7 month first day is Sunday!

  266. *************1974 year search result *********
  267. 1974 year 9 month first day is Sunday!
  268. 1974 year 12 month first day is Sunday!

  269. *************1975 year search result *********
  270. 1975 year 6 month first day is Sunday!

  271. *************1976 year search result *********
  272. 1976 year 2 month first day is Sunday!
  273. 1976 year 8 month first day is Sunday!

  274. *************1977 year search result *********
  275. 1977 year 5 month first day is Sunday!

  276. *************1978 year search result *********
  277. 1978 year 1 month first day is Sunday!
  278. 1978 year 10 month first day is Sunday!

  279. *************1979 year search result *********
  280. 1979 year 4 month first day is Sunday!
  281. 1979 year 7 month first day is Sunday!

  282. *************1980 year search result *********
  283. 1980 year 6 month first day is Sunday!

  284. *************1981 year search result *********
  285. 1981 year 2 month first day is Sunday!
  286. 1981 year 3 month first day is Sunday!
  287. 1981 year 11 month first day is Sunday!

  288. *************1982 year search result *********
  289. 1982 year 8 month first day is Sunday!

  290. *************1983 year search result *********
  291. 1983 year 5 month first day is Sunday!

  292. *************1984 year search result *********
  293. 1984 year 1 month first day is Sunday!
  294. 1984 year 4 month first day is Sunday!
  295. 1984 year 7 month first day is Sunday!

  296. *************1985 year search result *********
  297. 1985 year 9 month first day is Sunday!
  298. 1985 year 12 month first day is Sunday!

  299. *************1986 year search result *********
  300. 1986 year 6 month first day is Sunday!

  301. *************1987 year search result *********
  302. 1987 year 2 month first day is Sunday!
  303. 1987 year 3 month first day is Sunday!
  304. 1987 year 11 month first day is Sunday!

  305. *************1988 year search result *********
  306. 1988 year 5 month first day is Sunday!

  307. *************1989 year search result *********
  308. 1989 year 1 month first day is Sunday!
  309. 1989 year 10 month first day is Sunday!

  310. *************1990 year search result *********
  311. 1990 year 4 month first day is Sunday!
  312. 1990 year 7 month first day is Sunday!

  313. *************1991 year search result *********
  314. 1991 year 9 month first day is Sunday!
  315. 1991 year 12 month first day is Sunday!

  316. *************1992 year search result *********
  317. 1992 year 3 month first day is Sunday!
  318. 1992 year 11 month first day is Sunday!

  319. *************1993 year search result *********
  320. 1993 year 8 month first day is Sunday!

  321. *************1994 year search result *********
  322. 1994 year 5 month first day is Sunday!

  323. *************1995 year search result *********
  324. 1995 year 1 month first day is Sunday!
  325. 1995 year 10 month first day is Sunday!

  326. *************1996 year search result *********
  327. 1996 year 9 month first day is Sunday!
  328. 1996 year 12 month first day is Sunday!

  329. *************1997 year search result *********
  330. 1997 year 6 month first day is Sunday!

  331. *************1998 year search result *********
  332. 1998 year 2 month first day is Sunday!
  333. 1998 year 3 month first day is Sunday!
  334. 1998 year 11 month first day is Sunday!

  335. *************1999 year search result *********
  336. 1999 year 8 month first day is Sunday!
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-2 08:36:16 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2021-1-8 19:57 编辑
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;



  4. enum class Weekday :unsigned char {
  5.     Sunday,
  6.     Monday,
  7.     Tuesday,
  8.     Wednesday,
  9.     Thursday,
  10.     Friday,
  11.     Saturday
  12. };



  13. long long math_mod(long long a, long long b)noexcept {
  14.     return a - (long long)floor((long double)a / b) * b;
  15. }



  16. Weekday zeller(unsigned y, unsigned char m, unsigned char d)noexcept {
  17.     if (m <= 2) {
  18.         --y;
  19.         m += 12;
  20.     }

  21.     unsigned c = y / 100;
  22.     y -= c * 100;

  23.     return Weekday(math_mod(y + (y >> 2) + (c >> 2) - (c << 1) + ((13 * (m + 1)) / 5) + d - 1, 7));
  24. }



  25. int main() {
  26.     ios::sync_with_stdio(false);

  27.     unsigned count = 0;
  28.     unsigned char month;
  29.     unsigned short year;


  30.     for (year = 1901; year <= 2000; year++) {
  31.         for (month = 1; month <= 12; month++) {
  32.             if(zeller(year, month, 1) == Weekday::Sunday) {
  33.                 count++;
  34.             }
  35.         }
  36.     }


  37.     cout << count << endl;
  38.     return 0;
  39. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-20 19:53:40 | 显示全部楼层
  1. def run(n):
  2.     if n%100 != 0 and n%4==0:
  3.         return 29
  4.     elif n%100 == 0 and n%400==0:
  5.         return 29
  6.     else:
  7.         return 28

  8. n = 1901
  9. count = 365
  10. rz = [] #所有每个月第一天与1900 年 1 月 1 日相差余数为6的集合
  11. while n <= 2000:
  12.     dict1 = {1:31,2:run(n),3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
  13.     yue = 1
  14.     while yue <= 12:
  15.         count += dict1[yue]
  16.         if count%7 == 6:
  17.             rz.append(count)
  18.         yue += 1
  19.     n += 1

  20. print('20 世纪(1901 年 1 月 1 日到 2000 年 12 月 31 日)一共有 %s 个星期日落在了当月的第一天' % len(rz))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-8 18:07:03 | 显示全部楼层
  1. #include <stdio.h>

  2. main()
  3. {
  4.         int i, j, k, count = 0;
  5.         int day = 2;
  6.         for (i = 1901; i <= 2000; i++)
  7.         {
  8.                 if (day > 7)
  9.                 {
  10.                         day %= 7;
  11.                 }
  12.                 for (j = 1; j <= 12; j++)
  13.                 {
  14.                         k = j;
  15.                         switch (k)
  16.                         {
  17.                         case 1: k = 1; break;
  18.                         case 2: k = 32; break;
  19.                         case 3: k = 60; break;
  20.                         case 4: k = 91; break;
  21.                         case 5: k = 121; break;
  22.                         case 6: k = 152; break;
  23.                         case 7: k = 182; break;
  24.                         case 8: k = 213; break;
  25.                         case 9: k = 244; break;
  26.                         case 10: k = 274; break;
  27.                         case 11: k = 305; break;
  28.                         case 12: k = 335; break;
  29.                         }
  30.                         if (i % 4)
  31.                         {
  32.                                 k = k % 7;
  33.                                 k = k + day - 1;
  34.                                 if (k > 7)
  35.                                 {
  36.                                         k %= 7;
  37.                                 }
  38.                                 if (k == 7 || k == 0)
  39.                                 {
  40.                                         count++;
  41.                                 }
  42.                         }
  43.                         else
  44.                         {
  45.                                 if (j > 2)
  46.                                 {
  47.                                         k++;
  48.                                 }
  49.                                 k = k % 7;
  50.                                 k = k + day - 1;
  51.                                 if (k > 7)
  52.                                 {
  53.                                         k %= 7;
  54.                                 }
  55.                                 if (k == 7 || k == 0)
  56.                                 {
  57.                                         count++;                               
  58.                                 }
  59.                         }                       
  60.                 }
  61.                 if (i % 4)
  62.                 {
  63.                         day++;
  64.                 }
  65.                 else
  66.                 {
  67.                         day += 2;
  68.                 }
  69.         }
  70.         printf("\n%d", count);
  71.        
  72. }
复制代码

答案是: 171。若有改进的地方,希望大佬指出!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-29 13:57:22 | 显示全部楼层
本帖最后由 番杰 于 2021-10-29 14:06 编辑
  1. #include<stdio.h>
  2. //                  1  2  3  4  5  6  7  8  9 10 11 12
  3. const int Y[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
  4. typedef struct  {
  5.         year[1024];
  6.         month[1024];
  7. } YEAR;

  8. int main(void)
  9. {
  10.         int num = 0;
  11.         int sum = 366;
  12.         YEAR Year;
  13.        
  14.         for(int i = 1901;i<2001;i++)
  15.         {
  16.                 for(int j = 0;j <= 12;j++)
  17.                 {
  18.                         if((i % 4 == 0) && (j = 2))
  19.                         {       
  20.                                 sum += Y[j] + 1;
  21.                         }
  22.                         else
  23.                         {
  24.                                 sum += Y[j] ;
  25.                         }
  26.                        
  27.                         if(sum % 7 = 6)
  28.                         {                               
  29.                                 Year.year[num] = i;
  30.                                 Year.month[num] = j;
  31.                                 num++;       
  32.                         }
  33.                 }
  34.         }       
  35.                
  36.         printf("%d",num);
  37.        
  38.         return 0;
  39. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 07:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表