Python Learning: Program Control Structure 20141219
Programming Environment:
Windows 7 x64
Python 2.7.6
Topic:
1 Write the program to complete the following topics (1 points)
Topic content:
If a multiple of 3 or 5 of the natural number within 10 is listed, the 3,5,6,9 is included. Then the number of these numbers is 23. It is required to calculate the sum of the natural numbers of multiples of 3 or 5 within any positive integer n.
Input format:
A positive integer n.
Output format:
The sum of the natural numbers within N of multiples of 3 or 5.
Input Sample:
10
Sample output:
23
time limit: 500ms memory limit: 32000kb
n = Int (raw_input ()) sum = 0for i in range (0, N): If I% 3 = = 0 or i% 5 = = 0:sum + ielse:print sum
2. Write the procedure to complete the following topics (1 points)
Topic content:
The 2,3,5,7 of the prime number within 10 is 17. It is required to calculate the and of all primes within any positive integer n.
Input format:
A positive integer n.
Output format:
The and of all primes within N.
Input Sample:
10
Sample output:
17
Time limit: 500ms memory limit: 32000kb
Import mathn = Int (raw_input ()) sum = 0for tmp in xrange (2, N): For I in xrange (2, int (MATH.SQRT (TMP) + 1)): If T MP% i = = 0:break Else:sum + = Tmpprint sum
3 Write the program to complete the following topics (1 points)
Topic content:
Based on the following information, how many Sundays fell on the first day of the month between January 1, 1901 and December 31, 2000?
A) 1900.1.1 is Monday
b) January, March, May, July, August, October and December are 31 days
c) April, June, September and November are 30 days
D) February is 28 days, in leap years is 29 days
e) Year of A.D. can be divisible by 4 and can not be divisible by 100 is leap years
f) can be directly divisible by 400 is also a leap year
Output format:
A positive integer
time limit: 500ms memory limit: 32000kb
1). Self-groping out of:
days = 0weekday = 1year = 1900count = 0num = 0if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: days = 366else: days = 365weekday += days % 7for year in range (1901,2001): for month in range (1,13): if month in [1,3,5,7,8,10,12]: days = 31 weekday += days if weekday % 7 == 0: count += 1 elif month in [4,6,9,11]: days = 30 weekday += days if weekday % 7 == 0: count += 1 elif year % 4 == 0 and year % 100 != 0 or year % 400 == 0: days = 29 weekday += days if weekday % 7 == 0: count += 1 else: days = 28 weekday += days if weekday % 7 == 0 : count += 1print count
2.) After finishing the homework to see the more advanced answers:
Count=0year=1901month=[31,28,31,30,31,30,31,31,30,31,30,31]day= 1 + 365%7while Year < 2001:if (Year%4==0 and year% 100!=0) or year%400==0:month[1]=29 else:month[1]=28 for Mon in range (a): Day + = Month[mon] Day = day%7 # print (day,mon) if day = = 0:count + 1 year + = 1print (count)
4. Write the procedure to complete the following topics (2 points)
Topic content:
The number 197 can be referred to as the number of loops, since 197 of the three digit loops shift after the numbers: 197,971,719 are prime numbers. Within 100 such numbers include 13, 2,3,5,7,11,13,17,31,37,71,73,79,97. Requires a total number of such loops within any positive integer n .
Input format:
A positive integer n.
Output format:
The number of circular primes within N.
Input Sample:
100
Sample output:
13
time limit: 2000ms memory limit: 32000kb
Import mathcount = 0n = int (Raw_input ()) shuzi = 0if n >= 2: for shuzi in range (2,n+1): #shuzi = 197 for yinzi in range ( 2,int (Math.sqrt (Shuzi) + 1)): if shuzi % yinzi == 0: #print ' break: ',shuzi,yinzi break else: sushu = shuzi #print sushu sushu_tmp = sushu weishu = 0 while sushu_tmp != 0: weishu += 1 sushu_tmp /= 10 #print ' Weishu: ',weishu if weishu == 1: count += 1 elif weishu >= 2: # print sushu i = 1 while i < weishu: shuzi_xunhuan = int (sushu/(10**i) + (Sushu % (10**i) ) * (10 ** (weishu - i))) i += 1 for yinzi2 in range (2,int (math.sqrt (Shuzi_xunhuan) + 1)): &nbSp; if shuzi_xunhuan % yinzi2 == 0 : i = weishu + 1 break if i == weishu: count +=1 print count
This article from "Ops said: from rookie to veteran" blog, please be sure to keep this source http://liuqunying.blog.51cto.com/3984207/1591886
Python Learning: Program Control structure • Jobs 20141219