来自欧拉: 1900年1月1日是星期一。二十世纪的第一个月(1901年1月1日至2000年12月31日)有多少个星期日下降?
我的第一个函数每天收集一个列表,从已知的星期一开始到2000年12月31日结束。第二个函数将每七天保存到从1901年的第一个星期日开始的星期日列表中。
def dayListBuild():
dayListBuild = [[0,0,0]]
for year in range(1900, 2001):
for month in range(1, 13):
if month in [4, 6, 9, 11]:
dayRange = 30
elif not month - 2:
if year % 4 and (not year % 100 or year % 400):
dayRange = 29
else:
dayRange = 28
else:
dayRange = 31
for day in range(1, dayRange + 1):
dayListBuild.append([year, month, day])
return dayListBuild
def sundayList():
dayList = dayListBuild()
sundayList = []
for day in range(len(dayList)):
if not (day - 6) % 7 and dayList[day][0] > 1900 and not dayList[day][2] - 1:
sundayList.append(dayList[day])
return len(sundayList)
print(sundayList())
输出:200。 我的答案是29岁。所需答案是171。 这是我星期日的名单:
[[1901, 3, 1], [1901, 11, 1], [1902, 5, 1], [1903, 1, 1], [1903, 4, 1], [1903, 7, 1], [1904, 9, 1], [1904, 12, 1], [1905, 3, 1], [1905, 11, 1], [1906, 5, 1], [1907, 1, 1], [1907, 4, 1], [1907, 7, 1], [1908, 9, 1], [1908, 12, 1], [1909, 3, 1], [1909, 11, 1], [1910, 5, 1], [1911, 1, 1], [1911, 4, 1], [1911, 7, 1], [1912, 9, 1], [1912, 12, 1], [1913, 3, 1], [1913, 11, 1], [1914, 5, 1], [1915, 1, 1], [1915, 4, 1], [1915, 7, 1], [1916, 9, 1], [1916, 12, 1], [1917, 3, 1], [1917, 11, 1], [1918, 5, 1], [1919, 1, 1], [1919, 4, 1], [1919, 7, 1], [1920, 9, 1], [1920, 12, 1], [1921, 3, 1], [1921, 11, 1], [1922, 5, 1], [1923, 1, 1], [1923, 4, 1], [1923, 7, 1], [1924, 9, 1], [1924, 12, 1], [1925, 3, 1], [1925, 11, 1], [1926, 5, 1], [1927, 1, 1], [1927, 4, 1], [1927, 7, 1], [1928, 9, 1], [1928, 12, 1], [1929, 3, 1], [1929, 11, 1], [1930, 5, 1], [1931, 1, 1], [1931, 4, 1], [1931, 7, 1], [1932, 9, 1], [1932, 12, 1], [1933, 3, 1], [1933, 11, 1], [1934, 5, 1], [1935, 1, 1], [1935, 4, 1], [1935, 7, 1], [1936, 9, 1], [1936, 12, 1], [1937, 3, 1], [1937, 11, 1], [1938, 5, 1], [1939, 1, 1], [1939, 4, 1], [1939, 7, 1], [1940, 9, 1], [1940, 12, 1], [1941, 3, 1], [1941, 11, 1], [1942, 5, 1], [1943, 1, 1], [1943, 4, 1], [1943, 7, 1], [1944, 9, 1], [1944, 12, 1], [1945, 3, 1], [1945, 11, 1], [1946, 5, 1], [1947, 1, 1], [1947, 4, 1], [1947, 7, 1], [1948, 9, 1], [1948, 12, 1], [1949, 3, 1], [1949, 11, 1], [1950, 5, 1], [1951, 1, 1], [1951, 4, 1], [1951, 7, 1], [1952, 9, 1], [1952, 12, 1], [1953, 3, 1], [1953, 11, 1], [1954, 5, 1], [1955, 1, 1], [1955, 4, 1], [1955, 7, 1], [1956, 9, 1], [1956, 12, 1], [1957, 3, 1], [1957, 11, 1], [1958, 5, 1], [1959, 1, 1], [1959, 4, 1], [1959, 7, 1], [1960, 9, 1], [1960, 12, 1], [1961, 3, 1], [1961, 11, 1], [1962, 5, 1], [1963, 1, 1], [1963, 4, 1], [1963, 7, 1], [1964, 9, 1], [1964, 12, 1], [1965, 3, 1], [1965, 11, 1], [1966, 5, 1], [1967, 1, 1], [1967, 4, 1], [1967, 7, 1], [1968, 9, 1], [1968, 12, 1], [1969, 3, 1], [1969, 11, 1], [1970, 5, 1], [1971, 1, 1], [1971, 4, 1], [1971, 7, 1], [1972, 9, 1], [1972, 12, 1], [1973, 3, 1], [1973, 11, 1], [1974, 5, 1], [1975, 1, 1], [1975, 4, 1], [1975, 7, 1], [1976, 9, 1], [1976, 12, 1], [1977, 3, 1], [1977, 11, 1], [1978, 5, 1], [1979, 1, 1], [1979, 4, 1], [1979, 7, 1], [1980, 9, 1], [1980, 12, 1], [1981, 3, 1], [1981, 11, 1], [1982, 5, 1], [1983, 1, 1], [1983, 4, 1], [1983, 7, 1], [1984, 9, 1], [1984, 12, 1], [1985, 3, 1], [1985, 11, 1], [1986, 5, 1], [1987, 1, 1], [1987, 4, 1], [1987, 7, 1], [1988, 9, 1], [1988, 12, 1], [1989, 3, 1], [1989, 11, 1], [1990, 5, 1], [1991, 1, 1], [1991, 4, 1], [1991, 7, 1], [1992, 9, 1], [1992, 12, 1], [1993, 3, 1], [1993, 11, 1], [1994, 5, 1], [1995, 1, 1], [1995, 4, 1], [1995, 7, 1], [1996, 9, 1], [1996, 12, 1], [1997, 3, 1], [1997, 11, 1], [1998, 5, 1], [1999, 1, 1], [1999, 4, 1], [1999, 7, 1], [2000, 9, 1], [2000, 12, 1]]
我不确定我做错了什么。
答案 0 :(得分:3)
您使用的模数不正确。尝试:
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
答案 1 :(得分:0)
我只是想我想分享这个问题的代码,希望它可以帮助某人或更好,但有人可能想要批评它。
我知道有很多打印或者有些人认为不必要的打印正在进行,这有助于我理解控制台,所以如果你要对此发表评论,请保存一下。
public class CountingSundays
{
enum days {Thursday, Friday, Saturday, Sunday, Monday, Tuesday, Wednesday};
private int totalNumberDays, // help to carry the days
daysOfCurrentMonth, // check day of month
month, // track month
year, // track year
firstSundays; // count the number of first sundays
public static void main(String [] args)
{
// create an instance of CountingSundays
CountingSundays cs = new CountingSundays();
cs.year = 1901; // set the year
cs.totalNumberDays = 1; // set the day
while(cs.year < 2001)
{
// beginning of the year
cs.month = 1; // reset the months
// 30 days, Sept, April, June, Nov
// 31 days, Jan, Mar, May, Jul, Aug, Oct, Dec
// year % 4 == 0 and not 2000 as 2000 % 400 == 0,
// feb is 29, else its 28
while(cs.month < 13)
{
// beginning a new month
cs.daysOfCurrentMonth = 1;
// track the number of days per month
int monthlyDayCutOff = 1;
if( cs.month == 4 | // Apr
cs.month == 6 | // Jun
cs.month == 9 | // Sep
cs.month == 11 ) // Nov
monthlyDayCutOff = 30; // these months have 30
// if
else if(cs.month == 2)
{
// this is feb, check if it is a leap year
if(cs.year % 4 == 0 &&
cs.year % 100 != 0 ||
cs.year % 400 == 0)
monthlyDayCutOff = 29; // leap year
else
monthlyDayCutOff = 28; // not a leap year
} // else if
else
monthlyDayCutOff = 31; // one of the remaining months
// else
// use a while loop to calculate the days
while(cs.daysOfCurrentMonth < monthlyDayCutOff)
{
if(cs.daysOfCurrentMonth == 1 &
days.values()[cs.totalNumberDays % 7] == days.Sunday)
{
cs.firstSundays++; // increment first sundays
System.out.println("There is a first sunday in " +
cs.month);
} // if
// print current month date, and day
// the modulus helps cycle thru our enum
System.out.println(cs.daysOfCurrentMonth + " " +
days.values()[cs.totalNumberDays % 7]);
// print some statements
cs.daysOfCurrentMonth++; // increment days of month
cs.totalNumberDays++; // increment the total number of days
} // while days
// end of month
cs.month++;
System.out.println("\n\n" + cs.month);
} // while month
cs.year++;
} // while year
cs.year++;
// print money shot
System.out.println("Num of 1st sundays: " + cs.firstSundays);
} // main
} // CountingSundays