Python rewrite C language program 100 example -- Part3

Source: Internet
Author: User

'''[ Program 11] Question: classical problem: there is a rabbit. Every month since the first month of birth, a rabbit was born. After the third month, a rabbit was born every month, if rabbits do not die, how many rabbits are there each month? 1. program analysis: the rabbit rule is a sequence of numbers 1, 1, 2, 3, 5, 8, 13, 21 .... 2. program source code: main () {long f1, f2; int I; f1 = f2 = 1; for (I = 1; I <= 20; I ++) {printf ("% 12ld % 12ld", f1, f2); if (I % 2 = 0) printf ("\ n");/* controls the output, four lines per line */f1 = f1 + f2;/* values are assigned to the third month in the first two months */f2 = f1 + f2; /* Add the values of the first two months to the third month */} ''' f1 = 1f2 = 1for I in range ): print '% 12d % 12d' % (f1, f2) if (I % 2) = 0: print ''f1 = f1 + f2 f2 = f1 + f2

''' [Program 12] Question: Judge the number of prime numbers between and, and output all prime numbers. 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: ''' h = 0 leap = 1 from math import sqrtfrom sys import stdoutfor m in range (101,201): k = int (sqrt (m + 1 )) for I in range (2, k + 1): if m % I = 0: leap = 0 break if leap = 1: print '%-4d' % m h + = 1 if h % 10 = 0: print ''leap = 1 print 'the total is % d' % h

''' [Program 13]: print out all the "Daffodils". The so-called "Daffodils" refers to a three-digit number, each of which is equal to the number itself. For example, 153 is a "Daffodils" because 153 = the power of 1 + the power of 5 + the power of 3. 1. program analysis: The for loop is used to control the number of 100-999. Each number is used to calculate a single digit, ten digits, and hundreds of digits. 2. program source code: ''' for n in range ): I = n/100 j = n/10% 10 k = n % 10 if I * 100 + j * 10 + k = I + j ** 2 + k ** 3: print "%-5d" % n

''' [Program 14] Question: decomposing a positive integer into a prime factor. For example, enter 90 and print 90 = 2*3*3*5. Program Analysis: to break down the prime factor of n, first find a minimum prime number k, and then follow the steps below: (1) if the prime number is equal to n, it indicates that the process of decomposing the prime factor has been completed. Print it out. (2) If n <> k, but n can be divisible by k, the value of k should be printed, and n is divided by the quotient of k, as the new positive integer you n, repeat the first step. (3) If n cannot be divisible by k, k + 1 is used as the value of k and the first step is repeated. 2. program source code: ''' from sys import stdoutn = int (raw_input ("input number: \ n") print "n = % d" % nfor I in range (2, n + 1): while n! = I: if n % I = 0: stdout. write (str (I) stdout. write ("*") n = n/I else: breakprint "% d" % n

''' [Program 15] Question: Use nested conditional operators to complete this question: Students Whose scores> = 90 are represented by A, and students whose scores are between 60 and 89 are represented by B, the value below 60 is represented by C. 1. program analysis: (a> B )? A: B is a basic example of conditional operators. 2. program source code: The '''score = int (raw_input ('input score: \ n') operator is not supported. if score> = 90: grade = 'A' elif score> = 60: grade = 'B' else: grade = 'C' print '% d belongs to % s' % (score, grade)

''' [Program 17] Question: enter a line of characters to calculate the numbers of English letters, spaces, numbers, and other characters. 1. program Analysis: using the while statement, the condition is that the input character is not '\ n '. 2. program source code: ''' import strings = raw_input ('input a string: \ n') letters = 0 space = 0 digit = 0 others = 0for c in s: if c. isalpha (): letters + = 1 elif c. isspace (): space + = 1 elif c. isdigit (): digit + = 1 else: others + = 1 print 'Char = % d, space = % d, digit = % d, others = % d' % (letters, space, digit, others)

'''Question: calculate the value of s = a + aa + aaa + aaaa + aa... a, where a is a number. For example, 2 + 22 + 222 + 2222 + 22222 (a total of 5 numbers are added at this time), and the addition of several numbers is controlled by the keyboard. 1. program analysis: the key is to calculate the value of each item. 2. program source code: ''' Tn = 0Sn = [] n = int (raw_input ('n' =: \ n') a = int (raw_input ('a =: \ n') for count in range (n): Tn = Tn + a = a * 10 Sn. append (Tn) print TnSn = reduce (lambda x, y: x + y, Sn) print Sn

''' [Program 19] Question: If a number is equal to the sum of its factors, this number is called "complete number ". For example, 6 = 1 + 2 + 3. Program to find all the completion numbers within 1000. 1. program Analysis: see Program <-- Program 14 on the page. 2. program source code: ''' from sys import stdoutfor j in range (2,1001): k = [] n =-1 s = j for I in range (1, j ): if j % I = 0: n + = 1 s-= I k. append (I) if s = 0: print j for I in range (n): stdout. write (k [I]) stdout. write ('') print k [n]

 

''' [Program 20] Question: A ball falls freely from the height of 100 meters. After each landing, the ball jumps back to half of the original height. Then, when it falls down, how many meters are there? How high is the rebound of 10th times? 1. program Analysis: See note 2 below. program source code: ''' Sn = 100w.hn = Sn/2for n in range ): sn + = 2 * Hn/= 2 print 'total of road is % F' % Snprint 'The tenth is % fmeter' % Hn



 

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.