projecteuler---->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)

翻译:

现提供你如下信息,不过你可能希望自己印证一下。

  • 1900年1月1日是星期一
  • 除了二月外,四月,六月,九月,十一月,都有30天,其他月份都有31天。
  • 二月在平年有28天,在闰年有29天。
  • 能被4整除的年份是闰年,不过每个世纪年(??00)必须能被400整除才是闰年。

请问,在20世纪中(从1901年1月1日到2000年12月31日)总共有多少个月份的第一日是星期天?

def f(year,month):
  tmpDay=1
  if month == 1 or month == 2:
      month += 12
      year-=1
  if ((year<1752) or (year==1752 and month<9) or (year==1752 and month==9 and tmpDay<3)):
       a = (tmpDay + 2*month + 3*(month+1)/5 + year + year/4 +5) % 7;
  else:
       a = (tmpDay + 2*month + 3*(month+1)/5 + year + year/4 - year/100 + year/400)%7
  return a
resu=0
for i in range(1901,2001):
    for j in range(1,13):
       if f(i,j)==1 :
           resu+=1
print resu

projecteuler---->problem=19----Counting Sundays

时间: 2024-11-07 18:22:44

projecteuler---->problem=19----Counting Sundays的相关文章

project euler 19: Counting Sundays

import datetime count = 0 for y in range(1901,2001): for m in range(1,13): if datetime.datetime(y,m,1).weekday() == 6: count += 1 print count datetime此功能很好用.省去了计算的麻烦. count = 0 days = 1 year = 365 normal = [31,28,31,30,31,30,31,31,30,31,30,31] leap =

Project-Euler problem 1-50

最近闲的做了下Project Euler 上的题目,前面50题都比较简单,简单总结下.代码一般是Python和C/C++的 用Python 做这些题目简直是酸爽啊 一下代码可能不一定是我的,因为不知道论坛里面的回复不是永久的,所以我的代码有的丢了,可能找个和我的意思相近的代码.题目翻译是从 欧拉计划 | Project Euler 中文翻译站上面Copy 的表告我. Problem 1  Multiples of 3 and 5 10以下的自然数中,属于3和5的倍数的有3,5,6和9,它们之和是

欧拉计划(python) problem 19

Counting Sundays Problem 19 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 alon

SPOJ Problem 1724:Counting Triangles

题目大意:数三角形.. 数据范围是一百万,而且暴力不可行,所以要推公式.公式可以参照 http://www.docin.com/p-720073077.html #include<cstdio> int t; long long n,m,ans; int main(){ scanf("%d",&t); while(t--){ scanf("%lld",&n); ans=(n*(n+1)*(2*n+1))/6; m=(n-1)/2; if

Project Euler:Problem 76 Counting summations

It is possible to write five as a sum in exactly six different ways: 4 + 1 3 + 2 3 + 1 + 1 2 + 2 + 1 2 + 1 + 1 + 1 1 + 1 + 1 + 1 + 1 How many different ways can one hundred be written as a sum of at least two positive integers? #include <iostream> u

Project Euler:Problem 72 Counting fractions

Consider the fraction, n/d, where n and d are positive integers. If n<d and HCF(n,d)=1, it is called a reduced proper fraction. If we list the set of reduced proper fractions for d ≤ 8 in ascending order of size, we get: 1/8, 1/7, 1/6, 1/5, 1/4, 2/7,

hdu 2952 Counting Sheep

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2952 Counting Sheep Description A while ago I had trouble sleeping. I used to lie awake, staring at the ceiling, for hours and hours. Then one day my grandmother suggested I tried counting sheep after I'

【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2476    Accepted Submission(s): 1621 Problem Description A while ago I had trouble sleeping. I used to lie awake,

ACM HDU-2952 Counting Sheep A while ago I had trouble sleeping

Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2060    Accepted Submission(s): 1359 Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the c

HDOJ 2952 Counting Sheep

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2952 Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2231    Accepted Submission(s): 1474 Problem Description A while ago I had tro