Python rewrite C language program 100 -- Part9

Source: Internet
Author: User

'''Program 71: compile input () and output () function input, and output the data records of five students. 1. program Analysis: 2. program source code: Use list to simulate the structure (without class) stu = [string, string, list] ''' N = 3 # stu # num: string # name: string # score [4]: liststudent = [] for I in range (5): student. append (['','', []) def input_stu (stu): for I in range (N ): stu [I] [0] = raw_input ('input student num: \ n') stu [I] [1] = raw_input ('input student name: \ n ') for j in range (3): stu [I] [2]. append (int (raw_input ('score: \ n') def output_stu (stu): for I in range (n ): print '%-6 s %-10s' % (stu [I] [0], stu [I] [1]) for j in range (3 ): print '%-8d' % stu [I] [2] [j] if _ name _ = '_ main _': input_stu (student) print student output_stu (student)

'''Question: returns a linked list in reverse direction. 1. program Analysis: 2. program source code: '''if _ name _ = '_ main _': ptr = [] for I in range (5 ): num = int (raw_input ('Please input a number: \ n') ptr. append (num) print ptr. reverse () print ptr


''' [Program 72] Question: Create a linked list. 1. program Analysis: 2. program source code: '''if _ name _ = '_ main _': ptr = [] for I in range (5 ): num = int (raw_input ('Please input a number: \ n') ptr. append (num) print ptr


''' [Program 74] Question: connect two linked lists. 1. program Analysis: 2. program source code: the code seems to be only, list sorting ''' if _ name _ = '_ main _': arr1 = (, 11) ptr = list (arr1) print ptr. sort () print ptr


'''Program 75: Relax and calculate a simple question. 1. program Analysis: 2. program source code: ''' if _ name _ = '_ main _': for I in range (5): n = 0 if I! = 1: n + = 1 if I = 3: n + = 1 if I = 4: n + = 1 if I! = 4: n + = 1 if n = 3: print 64 + I


''' [Program 76] Question: compile a function. When n is an even number, call the function to calculate 1/2 + 1/4 +... + 1/n. When n is an odd number, call the function 1/1 + 1/3 +... + 1/n (using pointer functions) 1. program Analysis: 2. program source code: ''' def peven (n): I = 0 s = 0.0 for I in range (2, n + 1, 2 ): s + = 1.0/I return sdef podd (n): s = 0.0 for I in range (1, n +): s + = 1/I return sdef dcall (fp, n): s = fp (n) return sif _ name _ = '_ main _': n = int (raw_input ('input a number: \ n') if n % 2 = 0: sum = dcall (peven, n) else: sum = dcall (podd, n) print sum


''' [Program 77] Question: Fill in the blanks (pointer to pointer) 1. program Analysis: 2. program source code: main () {char * s [] = {"man", "woman", "girl", "boy", "sister"}; char ** q; int k; for (k = 0; k <5; k ++ ){; /* ***********************/printf ("% s \ n", * q );}} '''if _ name _ = '_ main _': s = ["man", "woman", "girl", "boy ", "sister"] for I in range (len (s): print s [I]


''' [Program 78] Question: Find the person of the greatest age and output it. Please find out what is wrong with the program. 1. program Analysis: 2. program source code ''' if _ name _ = '_ main _': person = {"li": 18, "wang": 50, "zhang ": 20, "sun": 22} m = 'lil' for key in person. keys (): if person [m] <person [key]: m = key print '% s, % d' % (m, person [m])


''' [Program 79] Question: String sorting. 1. program Analysis: 2. program source code: '''if _ name _ = '_ main _': str1 = raw_input ('input string: \ n ') str2 = raw_input ('input string: \ n') str3 = raw_input ('input string: \ n') print str1, str2, str3 if str1> str2: str1, str2 = str2, str1 if str1> str3: str1, str3 = str3, str1 if str2> str3: str2, str3 = str3, str2 print 'After being sorted. 'print str1, str2, str3


''' [Program 80] Question: There are a pile of peaches on the beach, and five monkeys will score points. The first monkey divided the pile of peach creden into five portions, one more. The monkey threw the other one into the sea and took one. The second monkey divides the remaining peaches into five equal portions and adds one more portion. It also throws one more portion into the sea and takes one portion, the third, fourth, and fifth monkeys did this. How many peaches did they have on the beach? 1. program Analysis: 2. program source code: ''' if _ name _ = '_ main _': for I in range (, 4 ): count = 0 m = I for k in range (5): j = I/4*5 + 1 I = j if j % 4 = 0: count + = 1 else: break I = m if count = 4: print count break



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.