题目19:20世纪有多少个星期日是当月的第一天?
Counting SundaysYou 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 日)一共有多少个星期日落在了当月的第一天?
目前电脑只有数据库环境,先用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 --! y = 1900
m = 1
xingqi = 1
day = 1
count = 0
def run(x):
if x%400 == 0:
return True
else:
if x%100 == 0:
return False
if x%4 == 0:
return True
return False
while y < 2001:
if m in :
if day == 31:
m += 1
day = 1
if m in :
if day == 32:
m += 1
day = 1
if m == 13:
y += 1
m = 1
if m == 2:
if run(y) == True:
if day == 30:
m += 1
day = 1
else:
if day == 29:
m += 1
day = 1
if xingqi == 8:
xingqi = 1
if y > 1900 and day == 1 and xingqi == 7:
count += 1
day += 1
xingqi += 1 本帖最后由 jerryxjr1220 于 2017-10-4 14:20 编辑
重新查证了一下,题目错了,1901年1月1日是星期二。
171
直接用datetime库
import datetime as dt
c = 0
for y in range(1901,2001):
for m in range(1,13):
dd = dt.datetime(y,m,1)
if dd.weekday()==6:
c += 1
print(c)
# encoding:utf-8
# 20世纪每月的第一天是星期天的有多少
'''
蔡勒(Zeller)公式W=Y++-2C++d-1
公式中的符号含义如下:W为星期数;C为世纪;Y为年(两位数);
M为月数(M=m(当m>2);M=m+12(m<3));d为日。
相比于通用通用计算公式而言,蔡勒(Zeller)公式大大降低了计算的复杂度。
'''
from time import time
def euler019():
count = 0
for year in range(1901, 2001):
for month in range(1, 13):
temp = str(year) + '-' + str(month) + '-' + '1'
# 1、2月份,看做上一年的13、14月份
if month in :
M = month + 12
Y = year - 1
else:
M = month
Y = year
# 世纪数年份的前两位
C = int(year / 100)
# 年年份的后两位
Y = Y % 100
# 日日期数 本题默认为1
D = 1
W = (Y + int(Y / 4) + int(C / 4) - 2 * C + int(26 * (M + 1) / 10) + D - 1) % 7
if not W:
count += 1
#print(temp)
return count
if __name__ == '__main__':
start = time()
print(euler019())
print('cost %.6f sec' % (time() - start))
171
cost 0.003001 sec
此代码使用matlab编程
Problem19所用时间为0.0055557秒
Problem19的答案为171
%题目19:20世纪有多少个星期日是当月的第一天?
function Output=Problem19(Input)
tic
if nargin==0
Input=7;%星期天
end
Common=;%平年序列
Leap=;%闰年序列
Century=[];%一个世纪的时间序列
Sum=0;%当月第一天为星期天的总天数
for ii=1901:2000
if mod(ii,4)==0
Temp=;
else
Temp=;
end
Century=Temp;
end
L=length(Century);%世纪的长度
Week=1:7;
if mod(L,7)==0
Weekday=repmat(Week,);%星期时间序列
else
Weekday=repmat(Week,);
end
Weekday=Weekday(2:end);%1901年1月1日是星期2
for jj=1:L
if Weekday(jj)==Input&&Century(jj)==1%当天为星期天的同时,也是当月的第一天
Sum=Sum+1;
end
end
toc
Output=Sum;
disp('此代码使用matlab编程')
disp(['Problem19所用时间为',num2str(toc),'秒'])
disp(['Problem19的答案为',num2str(Output)])
end
number = 0
day = 1
days =
def isleap(_year):
if (_year % 4 == 0 and _year % 100 != 0) or (_year % 400 == 0):
return True
else:
return False
for year in range(1900,2001):
if isleap(year):
days = 29
else:
days = 28
for i in days:
day += i
if day % 7 == 0 and year >= 1901:
number += 1
print number jerryxjr1220 发表于 2016-10-11 15:48
重新查证了一下,题目错了,1901年1月1日是星期二。
171
看清楚题目。。题目说的是1900年1月1日是星期一。。。
然后问题说的是从1901年1月1日开始算起。。。 171
month=
sunday=0
xingqi=1
xingqi=(xingqi+365%7)%7
for years in range (1901,2001):
if years%4==0:
month=29
month=28
for i in month:
xingqi=(xingqi+i%7)%7
if xingqi==0:
sunday+=1
print(sunday) 本帖最后由 99592938 于 2017-3-15 11:05 编辑
wek = (1+365)%7 #1901-01-01是星期几
list_sun=[]
mon=
for year in range(1901,2001):
if year%4:mon=28
else:mon=29
for m in range(12):
if wek==0:list_sun.append('%d-%d' %(year,m+1))
wek=(wek+mon)%7
print(len(list_sun))
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'] #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;
} #include <iostream>
using namespace std;
int main()
{
const short NYEAR={0,31,28,31,30,31,30,31,31,30,31,30,31};
const short RYEAR={0,31,29,31,30,31,30,31,31,30,31,30,31};
int day=6,month=1,sum=0;
for(int year=1901;year<=2000;++year)
{
if(year%4==0&&year%100!=0||year%400==0)
{
while(month<=12)
{
day+=7;
if(day>=RYEAR)
{
day-=RYEAR;
++month;
if(day==1)
{
++sum;
}
}
}
month=1;
}
else
{
while(month<=12)
{
day+=7;
if(day>=NYEAR)
{
day-=NYEAR;
++month;
if(day==1)
{
++sum;
}
}
}
month=1;
}
}
cout<<"总共有"<<sum<<"个星期日是每个月的第一天。";
} python3 时间为0.001526 s
结果是171
import time
start = time.clock()
yearn = {1: 3, 2: 0, 3: 3, 4: 2, 5: 3, 6: 2,
7: 3, 8: 3, 9: 2, 10: 3, 11: 2, 12: 3}
yearr = {1: 3, 2: 1, 3: 3, 4: 2, 5: 3, 6: 2,
7: 3, 8: 3, 9: 2, 10: 3, 11: 2, 12: 3}
count = 0
daysum = 2
n = 13
for y in range(1901, 2001):
if ((y % 4 == 0 and y % 100 != 0) or y % 400 == 0):
year = yearr
else:
year = yearn
if y == 2000:
n = 12
for m in range(1, n):
daysum = (daysum + year) % 7
if daysum == 0:
if m + 1 == 13:
print("%d-%d-1" % (y + 1, 1))
else:
print("%d-%d-1" % (y, m + 1))
count += 1
print("共有%d个" % count)
end = time.clock()
print("read:%f s" % (end - start))
答案是171
mport time
import math
def test1():
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}
wed = 1
for m in range(1900,2001):
if (m%4==0 and m%100!=0) or (m%400==0):
leap = 1
else:
leap = 0
for n in range(1,13):
if n == 2:
if leap == 1:
mon = 29
else:
mon = 28
for i in range(1,mon+1):
if wed == 8:
wed = 1
if i == 1 and wed == 7:
if m != 1900:
li.append()
wed += 1
return li,len(li)
li = []
start = time.perf_counter()
print(test1())
end = time.perf_counter()
print(end-start) jerryxjr1220 发表于 2016-10-11 15:48
重新查证了一下,题目错了,1901年1月1日是星期二。
171
题目说的是1990年1月1日是星期一 本帖最后由 guoquanli 于 2020-1-10 13:13 编辑
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
//是否是润年i的判断
bool IsLeapYear(int year){
if((year%4 == 0 && year%100 != 0) || year %400 == 0){
return true;
}else{
return false;
}
}
//测试函数
void testIsLeapYear(int year){
if(IsLeapYear(year)){
printf("%d year is leap year!\n",year);
}else{
printf("%d year is not leap year!\n",year);
}
}
// 判断从1900年到year年总共有多少天
int DayCal(int year){
int sum = 0;
for(int i = 1900; i < year; i++){
if(IsLeapYear(i)){
sum += 366;
}else{
sum += 365;
}
}
return sum;
}
//测试函数
void testDayCal(int year){
printf("from 1900 to this year,toal day :%d\n",DayCal(year));
}
//统计输入的年份中,当月第一天是星期日
void FindMonth(int year){
int sum= DayCal(year);
bool flag = IsLeapYear(year);
int dayArray = {0,31,28,31,30,31,30,31,31,30,31,30};
if(flag){
dayArray = 29;
}
for(int i = 0; i<12;i++){
sum += dayArray;
if((sum+ 1) % 7 == 0){
printf("%d year %d month first day is Sunday!\n",year,i+1);
}
}
}
void main(){
for(int i = 1900; i<2000;i++){
printf("*************%d year search result *********\n",i);
FindMonth(i);
printf("\n");
}
}
======运行时间======
real 0m0.005s
user 0m0.000s
sys 0m0.005s
======运行结果======
*************1900 year search result *********
1900 year 4 month first day is Sunday!
1900 year 7 month first day is Sunday!
*************1901 year search result *********
1901 year 9 month first day is Sunday!
1901 year 12 month first day is Sunday!
*************1902 year search result *********
1902 year 6 month first day is Sunday!
*************1903 year search result *********
1903 year 2 month first day is Sunday!
1903 year 3 month first day is Sunday!
1903 year 11 month first day is Sunday!
*************1904 year search result *********
1904 year 5 month first day is Sunday!
*************1905 year search result *********
1905 year 1 month first day is Sunday!
1905 year 10 month first day is Sunday!
*************1906 year search result *********
1906 year 4 month first day is Sunday!
1906 year 7 month first day is Sunday!
*************1907 year search result *********
1907 year 9 month first day is Sunday!
1907 year 12 month first day is Sunday!
*************1908 year search result *********
1908 year 3 month first day is Sunday!
1908 year 11 month first day is Sunday!
*************1909 year search result *********
1909 year 8 month first day is Sunday!
*************1910 year search result *********
1910 year 5 month first day is Sunday!
*************1911 year search result *********
1911 year 1 month first day is Sunday!
1911 year 10 month first day is Sunday!
*************1912 year search result *********
1912 year 9 month first day is Sunday!
1912 year 12 month first day is Sunday!
*************1913 year search result *********
1913 year 6 month first day is Sunday!
*************1914 year search result *********
1914 year 2 month first day is Sunday!
1914 year 3 month first day is Sunday!
1914 year 11 month first day is Sunday!
*************1915 year search result *********
1915 year 8 month first day is Sunday!
*************1916 year search result *********
1916 year 10 month first day is Sunday!
*************1917 year search result *********
1917 year 4 month first day is Sunday!
1917 year 7 month first day is Sunday!
*************1918 year search result *********
1918 year 9 month first day is Sunday!
1918 year 12 month first day is Sunday!
*************1919 year search result *********
1919 year 6 month first day is Sunday!
*************1920 year search result *********
1920 year 2 month first day is Sunday!
1920 year 8 month first day is Sunday!
*************1921 year search result *********
1921 year 5 month first day is Sunday!
*************1922 year search result *********
1922 year 1 month first day is Sunday!
1922 year 10 month first day is Sunday!
*************1923 year search result *********
1923 year 4 month first day is Sunday!
1923 year 7 month first day is Sunday!
*************1924 year search result *********
1924 year 6 month first day is Sunday!
*************1925 year search result *********
1925 year 2 month first day is Sunday!
1925 year 3 month first day is Sunday!
1925 year 11 month first day is Sunday!
*************1926 year search result *********
1926 year 8 month first day is Sunday!
*************1927 year search result *********
1927 year 5 month first day is Sunday!
*************1928 year search result *********
1928 year 1 month first day is Sunday!
1928 year 4 month first day is Sunday!
1928 year 7 month first day is Sunday!
*************1929 year search result *********
1929 year 9 month first day is Sunday!
1929 year 12 month first day is Sunday!
*************1930 year search result *********
1930 year 6 month first day is Sunday!
*************1931 year search result *********
1931 year 2 month first day is Sunday!
1931 year 3 month first day is Sunday!
1931 year 11 month first day is Sunday!
*************1932 year search result *********
1932 year 5 month first day is Sunday!
*************1933 year search result *********
1933 year 1 month first day is Sunday!
1933 year 10 month first day is Sunday!
*************1934 year search result *********
1934 year 4 month first day is Sunday!
1934 year 7 month first day is Sunday!
*************1935 year search result *********
1935 year 9 month first day is Sunday!
1935 year 12 month first day is Sunday!
*************1936 year search result *********
1936 year 3 month first day is Sunday!
1936 year 11 month first day is Sunday!
*************1937 year search result *********
1937 year 8 month first day is Sunday!
*************1938 year search result *********
1938 year 5 month first day is Sunday!
*************1939 year search result *********
1939 year 1 month first day is Sunday!
1939 year 10 month first day is Sunday!
*************1940 year search result *********
1940 year 9 month first day is Sunday!
1940 year 12 month first day is Sunday!
*************1941 year search result *********
1941 year 6 month first day is Sunday!
*************1942 year search result *********
1942 year 2 month first day is Sunday!
1942 year 3 month first day is Sunday!
1942 year 11 month first day is Sunday!
*************1943 year search result *********
1943 year 8 month first day is Sunday!
*************1944 year search result *********
1944 year 10 month first day is Sunday!
*************1945 year search result *********
1945 year 4 month first day is Sunday!
1945 year 7 month first day is Sunday!
*************1946 year search result *********
1946 year 9 month first day is Sunday!
1946 year 12 month first day is Sunday!
*************1947 year search result *********
1947 year 6 month first day is Sunday!
*************1948 year search result *********
1948 year 2 month first day is Sunday!
1948 year 8 month first day is Sunday!
*************1949 year search result *********
1949 year 5 month first day is Sunday!
*************1950 year search result *********
1950 year 1 month first day is Sunday!
1950 year 10 month first day is Sunday!
*************1951 year search result *********
1951 year 4 month first day is Sunday!
1951 year 7 month first day is Sunday!
*************1952 year search result *********
1952 year 6 month first day is Sunday!
*************1953 year search result *********
1953 year 2 month first day is Sunday!
1953 year 3 month first day is Sunday!
1953 year 11 month first day is Sunday!
*************1954 year search result *********
1954 year 8 month first day is Sunday!
*************1955 year search result *********
1955 year 5 month first day is Sunday!
*************1956 year search result *********
1956 year 1 month first day is Sunday!
1956 year 4 month first day is Sunday!
1956 year 7 month first day is Sunday!
*************1957 year search result *********
1957 year 9 month first day is Sunday!
1957 year 12 month first day is Sunday!
*************1958 year search result *********
1958 year 6 month first day is Sunday!
*************1959 year search result *********
1959 year 2 month first day is Sunday!
1959 year 3 month first day is Sunday!
1959 year 11 month first day is Sunday!
*************1960 year search result *********
1960 year 5 month first day is Sunday!
*************1961 year search result *********
1961 year 1 month first day is Sunday!
1961 year 10 month first day is Sunday!
*************1962 year search result *********
1962 year 4 month first day is Sunday!
1962 year 7 month first day is Sunday!
*************1963 year search result *********
1963 year 9 month first day is Sunday!
1963 year 12 month first day is Sunday!
*************1964 year search result *********
1964 year 3 month first day is Sunday!
1964 year 11 month first day is Sunday!
*************1965 year search result *********
1965 year 8 month first day is Sunday!
*************1966 year search result *********
1966 year 5 month first day is Sunday!
*************1967 year search result *********
1967 year 1 month first day is Sunday!
1967 year 10 month first day is Sunday!
*************1968 year search result *********
1968 year 9 month first day is Sunday!
1968 year 12 month first day is Sunday!
*************1969 year search result *********
1969 year 6 month first day is Sunday!
*************1970 year search result *********
1970 year 2 month first day is Sunday!
1970 year 3 month first day is Sunday!
1970 year 11 month first day is Sunday!
*************1971 year search result *********
1971 year 8 month first day is Sunday!
*************1972 year search result *********
1972 year 10 month first day is Sunday!
*************1973 year search result *********
1973 year 4 month first day is Sunday!
1973 year 7 month first day is Sunday!
*************1974 year search result *********
1974 year 9 month first day is Sunday!
1974 year 12 month first day is Sunday!
*************1975 year search result *********
1975 year 6 month first day is Sunday!
*************1976 year search result *********
1976 year 2 month first day is Sunday!
1976 year 8 month first day is Sunday!
*************1977 year search result *********
1977 year 5 month first day is Sunday!
*************1978 year search result *********
1978 year 1 month first day is Sunday!
1978 year 10 month first day is Sunday!
*************1979 year search result *********
1979 year 4 month first day is Sunday!
1979 year 7 month first day is Sunday!
*************1980 year search result *********
1980 year 6 month first day is Sunday!
*************1981 year search result *********
1981 year 2 month first day is Sunday!
1981 year 3 month first day is Sunday!
1981 year 11 month first day is Sunday!
*************1982 year search result *********
1982 year 8 month first day is Sunday!
*************1983 year search result *********
1983 year 5 month first day is Sunday!
*************1984 year search result *********
1984 year 1 month first day is Sunday!
1984 year 4 month first day is Sunday!
1984 year 7 month first day is Sunday!
*************1985 year search result *********
1985 year 9 month first day is Sunday!
1985 year 12 month first day is Sunday!
*************1986 year search result *********
1986 year 6 month first day is Sunday!
*************1987 year search result *********
1987 year 2 month first day is Sunday!
1987 year 3 month first day is Sunday!
1987 year 11 month first day is Sunday!
*************1988 year search result *********
1988 year 5 month first day is Sunday!
*************1989 year search result *********
1989 year 1 month first day is Sunday!
1989 year 10 month first day is Sunday!
*************1990 year search result *********
1990 year 4 month first day is Sunday!
1990 year 7 month first day is Sunday!
*************1991 year search result *********
1991 year 9 month first day is Sunday!
1991 year 12 month first day is Sunday!
*************1992 year search result *********
1992 year 3 month first day is Sunday!
1992 year 11 month first day is Sunday!
*************1993 year search result *********
1993 year 8 month first day is Sunday!
*************1994 year search result *********
1994 year 5 month first day is Sunday!
*************1995 year search result *********
1995 year 1 month first day is Sunday!
1995 year 10 month first day is Sunday!
*************1996 year search result *********
1996 year 9 month first day is Sunday!
1996 year 12 month first day is Sunday!
*************1997 year search result *********
1997 year 6 month first day is Sunday!
*************1998 year search result *********
1998 year 2 month first day is Sunday!
1998 year 3 month first day is Sunday!
1998 year 11 month first day is Sunday!
*************1999 year search result *********
1999 year 8 month first day is Sunday!
本帖最后由 永恒的蓝色梦想 于 2021-1-8 19:57 编辑
#include<iostream>
#include<cmath>
using namespace std;
enum class Weekday :unsigned char {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};
long long math_mod(long long a, long long b)noexcept {
return a - (long long)floor((long double)a / b) * b;
}
Weekday zeller(unsigned y, unsigned char m, unsigned char d)noexcept {
if (m <= 2) {
--y;
m += 12;
}
unsigned c = y / 100;
y -= c * 100;
return Weekday(math_mod(y + (y >> 2) + (c >> 2) - (c << 1) + ((13 * (m + 1)) / 5) + d - 1, 7));
}
int main() {
ios::sync_with_stdio(false);
unsigned count = 0;
unsigned char month;
unsigned short year;
for (year = 1901; year <= 2000; year++) {
for (month = 1; month <= 12; month++) {
if(zeller(year, month, 1) == Weekday::Sunday) {
count++;
}
}
}
cout << count << endl;
return 0;
} def run(n):
if n%100 != 0 and n%4==0:
return 29
elif n%100 == 0 and n%400==0:
return 29
else:
return 28
n = 1901
count = 365
rz = [] #所有每个月第一天与1900 年 1 月 1 日相差余数为6的集合
while n <= 2000:
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}
yue = 1
while yue <= 12:
count += dict1
if count%7 == 6:
rz.append(count)
yue += 1
n += 1
print('20 世纪(1901 年 1 月 1 日到 2000 年 12 月 31 日)一共有 %s 个星期日落在了当月的第一天' % len(rz)) #include <stdio.h>
main()
{
int i, j, k, count = 0;
int day = 2;
for (i = 1901; i <= 2000; i++)
{
if (day > 7)
{
day %= 7;
}
for (j = 1; j <= 12; j++)
{
k = j;
switch (k)
{
case 1: k = 1; break;
case 2: k = 32; break;
case 3: k = 60; break;
case 4: k = 91; break;
case 5: k = 121; break;
case 6: k = 152; break;
case 7: k = 182; break;
case 8: k = 213; break;
case 9: k = 244; break;
case 10: k = 274; break;
case 11: k = 305; break;
case 12: k = 335; break;
}
if (i % 4)
{
k = k % 7;
k = k + day - 1;
if (k > 7)
{
k %= 7;
}
if (k == 7 || k == 0)
{
count++;
}
}
else
{
if (j > 2)
{
k++;
}
k = k % 7;
k = k + day - 1;
if (k > 7)
{
k %= 7;
}
if (k == 7 || k == 0)
{
count++;
}
}
}
if (i % 4)
{
day++;
}
else
{
day += 2;
}
}
printf("\n%d", count);
}
答案是: 171。若有改进的地方,希望大佬指出! 本帖最后由 番杰 于 2021-10-29 14:06 编辑
#include<stdio.h>
// 123456789 10 11 12
const int Y[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
typedef struct{
year;
month;
} YEAR;
int main(void)
{
int num = 0;
int sum = 366;
YEAR Year;
for(int i = 1901;i<2001;i++)
{
for(int j = 0;j <= 12;j++)
{
if((i % 4 == 0) && (j = 2))
{
sum += Y + 1;
}
else
{
sum += Y ;
}
if(sum % 7 = 6)
{
Year.year = i;
Year.month = j;
num++;
}
}
}
printf("%d",num);
return 0;
}
页:
[1]
2