Python rewrite C language program 100 example -- Part4

Source: Internet
Author: User

''' [Program 24] Question: There is a fractional sequence: 2/1, 3/2, 5/3, 8/5, 13/8... find the sum of the first 20 items of this series. 1. Program Analysis: grasp the variation rules between numerator and denominator. 2. program source code: ''' # method 1 a = 2.0b = 1.0 s = 0for n in range (1, 21 ): s + = a/B t = a + B = tprint s # method 2 s = 0.0for n in range (): s + = a/B, a = a, a + bprint ss = 0.0for n in range (1, 21): s + = a/B, a =, a + bprint s # method 3 l = [] for n in range (1, 21): B, a = a, a + B l. append (a/B) print reduce (lambda x, y: x + y, l)

''' [Program 21] Question: monkeys eat peaches. The monkey picked several peaches on the first day and immediately ate half of them, I eat another one, and then I eat half of the peaches the next morning. In the future, I eat the remaining half of the previous day every morning. When you want to eat again in the morning of the seventh day, there is only one peach left. Calculate the total number of extracted items on the first day. 1. program analysis: the reverse thinking method is used to infer from the back. 2. program source code: ''' x2 = 1for day in range (9, 0,-1): x1 = (x2 + 1) * 2x2 = x1print x1

''' [Program 22] Question: two table tennis teams are playing, each of which has three members. Team a is a, B, and c, and Team B is x, y, and z. The game list has been determined by lottery. Someone asked the players about the game list. A said that he is not in comparison with x, c said that he is not in comparison with x and z. please compile a program to find a list of three-team players. 1. Program Analysis: Method for Determining prime numbers: Remove 2 to sqrt (This number) with a single number. If it can be divisible, it indicates that this number is not a prime number, and vice versa. 2. program source code: ''' for I in range (ord ('x'), ord ('Z') + 1): for j in range (ord ('x '), ord ('Z') + 1): if I! = J: for k in range (ord ('x'), ord ('Z') + 1): if (I! = K) and (j! = K): if (I! = Ord ('x') and (k! = Ord ('x') and (k! = Ord ('Z'): print 'order is a -- % s \ t B -- % s \ tc -- % s' % (chr (I ), chr (j), chr (k ))

''' [Program 23] Question: print the example (diamond) * ************************* 1. program Analysis: divide the graph into two parts. The first four rows have a rule, and the last three rows have a rule. The dual for loop is used to control the row at the first layer and control the column at the second layer. 2. program source code: ''' from sys import stdoutfor I in range (4): for j in range (2-I + 1): stdout. write ('') for k in range (2 * I + 1): stdout. write ('*') printfor I in range (3): for j in range (I + 1): stdout. write ('') for k in range (4-2 * I + 1): stdout. write ('*') print

 

''' [Program 25] Question: 1 + 2! + 3! +... + 20! And 1. Program Analysis: This program only turns accumulation into a multiplication. 2. program source code: ''' # method n = 0 s = 0 t = 1for n in range (1, 21): t * = n s + = tprint '1! + 2! + 3! +... + 20! = % D '% s # method 2 s = 0l = range (1, 21) def op (x): r = 1 for I in range (1, x + 1 ): r * = I return rs = sum (map (op, l) print '1! + 2! + 3! +... + 20! = % D '% s


 

''' [Program 26] Question: Use recursive method to calculate 5 !. 1. Program Analysis: recursive formula: fn = fn_1 * 4! 2. program source code: ''' def fact (j): sum = 0 if j = 0: sum = 1 else: sum = j * fact (j-1) return sumfor I in range (5): print '% d! = % D' % (I, fact (I ))

 

''' [Program 27] Question: print the five characters in reverse order using recursive function call. 1. program Analysis: 2. program source code: ''' def palin (n): next = 0 if n <= 1: next = input () print next else: next = input () palin (n-1) print nexti = 5 palin (I) print


 

''' [Program 28] Question: How old are five people sitting together? He said he is two years older than 4th. Asked about the age of 4th People, he said he was two years older than 3rd people. Ask the third person, and say that the person is two years older than 2nd. Ask 2nd people, saying they are two years older than the first. Finally, I asked the first person that he was 10 years old. How old is the fifth person? 1. Program Analysis: Using recursive methods, recursion is divided into two stages: push-back and recursion. To know the age of the fifth person, you need to know the age of the fourth person, and so on, and push it back to the first person (10 years old. '''Def age (n): if n = 1: c = 10 else: c = age (n-1) + 2 return cprint age (5)


 

''' [Program 29] Question: to give a positive integer of no more than five digits, requirement: 1. Calculate the number of digits. 2. Print the numbers in reverse order. 1. program Analysis: I learned to break down every single digit, as explained below: (this is a simple algorithm provided by Zhao Xin, class 002 of the Teacher's College) 2. program source code: ''' x = int (raw_input ("input a number: \ n ")) a = x/1_ B = x % 10000/1000 c = x % 1000/100 d = x % 100/10 e = x % 10if! = 0: print "there are 5", e, d, c, B, aelif B! = 0: print "there are 4", d, c, B, aelif c! = 0: print "there are 3", e, d, celif d! = 0: print "there are 2", e, delse: print "there are 1", e


 

'''Question: a five-digit query to determine whether it is a return number. That is to say, 12321 is the number of replies, with the same number of digits and tens of thousands of digits. 1. program Analysis: 29 cases 2. program source code: ''' x = int (raw_input ("input a number: \ n") x = str (x) for I in range (len (x)/2): if x [I]! = X [-I-1]: print 'This number is not a huiwen 'breakprint' this number is a huiwen'



 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.