本帖最后由 WylLy 于 2016-6-2 07:24 编辑
20 世纪(1901 年 1 月 1 日到 2000 年 12 月 31 日)一共有多少个星期日落在了当月的第一天?
- #coding:utf-8
- import time
- def last_day(day):
- global month_last_day_week
- while day:
- if month_last_day_week == 7:
- month_last_day_week = 1
- else:
- month_last_day_week += 1
- day -= 1
- lt = []
- month_last_day_week = 7
- t1 = time.time()
- for year in range(1900, 2001):
- for month in range(1, 13):
- if month_last_day_week == 6:
- lt.append(str(year) + '-' + str(month))
- if month in [4,6,9,11]:
- day = 2 #减去28后的值
- elif month == 2:
- day = 8 if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) else 7 #减去21后的值
- else:
- day = 3 #减去28后的值
- last_day(day)
- t2 = time.time()
- print('一共有:',len(lt))
- print('它们是:',lt)
- print('运行所耗的时间是:', (t2-t1))
复制代码
结果:
- 一共有: 173
- 它们是: ['1900-4', '1900-7', '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']
- 运行所耗的时间是: 0.002001047134399414
复制代码
附加题
- import math
- import time
- t3 = time.time()
- for i in range(100):
- d = math.factorial(100)
- result = 0
- for i in str(d):
- result += int(i)
- t4 = time.time()
- print('100! 的各位之和是:',result)
- print('运行100次所耗的时间是:',(t4-t3))
复制代码
附加题结果:
- 100! 的各位之和是: 648
- 运行100次所耗的时间是: 0.008004903793334961
复制代码 |