Python rewrite C language program 100 example -- Part2

Source: Internet
Author: User

'''Question: enter a day of a certain year to determine the day of the year? 1. program Analysis: Taking June 1, March 5 as an example, we should add up the previous two months, and then add 5 days, that is, the day of the current year. In special cases, an additional day is required when the input month is greater than 3 in a leap year. 2. source code: ''' year = int (raw_input ('year: \ n') month = int (raw_input ('month: \ n ')) day = int (raw_input ('day: \ n') months = (120,151,181,212,243,273,304,334,) if 0 <= month <= 12: sum = months [month-1] else: print 'data error' sum + = dayleap = 0if (year % 400 = 0) or (year % 4 = 0) and (year % 100! = 0): leap = 1if (leap = 1) and (month> 2): sum + = 1 print 'it is the % dth day. '% sum

''' [Program 5] Question: Enter three integers x, y, and z. Please output these three numbers from small to large. 1. program Analysis: we try to put the smallest number on x and compare x with y first. If x> y, the values of x and y are exchanged, then compare x with z. If x> z, the value of x is exchanged with z to minimize x. 2. program source code: ''' l = [] for I in range (3): x = int (raw_input ('integer: \ n') l. append (x) l. sort () print l

 

''' [Program 6] Question: Use * to output the C Letter. 1. Program Analysis: You can use the '*' sign to write the letter C on the paper, and then output it by branch. 2. program source code: '''print 'Hello Python world! \ N 'print '* 10for I in range (5): print' ** 'print '* 10 print' * \ n' * 6

 

''' [Program 7] Question: Output special pattern. Please run it in the c environment. Have a look, Very Beautiful! 1. Program Analysis: There are 256 characters in total. Different Characters and images are different. 2. program source code: '''a = 176b = 219 print chr (B), chr (a), chr (a), chr (B) print chr (a), chr (B), chr (a), chr (B), chr (a) print chr (a), chr (), chr (B), chr (a), chr (a) print chr (a), chr (B), chr (a), chr (B), chr () print chr (B), chr (a), chr (B)


 

'''Program 8: Output 9*9 tips. 1. Program Analysis: For branch and column consideration, there are 9 rows and 9 columns, I control row and j control column. 2. program source code: # include "stdio. h "main () {int I, j, result; printf (" \ n "); for (I = 1; I <10; I ++) {for (j = 1; j <10; j ++) {result = I * j; printf ("% d * % d = %-3d", I, j, result);/*-3d indicates the left alignment, which occupies 3 places */} printf ("\ n "); /* line feed after each line */} ''' for I in range (): for j in range ): result = I * j print '% d * % d = %-3d' % (I, j, result) print''


 

''' [Program 9] Question: It is required to output the chess board. 1. Program Analysis: Use I control rows and j to control columns, and control the output black square or white square according to the sum of I + j. 2. program source code: # include "stdio. h "main () {int I, j; for (I = 0; I <8; I ++) {for (j = 0; j <8; j ++) if (I + j) % 2 = 0) printf ("% c", 219,219); else printf (""); printf ("\ n") ;}}''' import sysfor I in range (8): for j in range (8): if (I + j) % 2 = 0: sys. stdout. write (chr (219) sys. stdout. write (chr (219) else: sys. stdout. write ('') print''


 

'''Program 10: print the stairs and print two smiling faces above the stairs. 1. Program Analysis: Use I to control rows and j to control columns. j controls the number of black squares output based on I changes. 2. program source code: ''' import syssys. stdout. write (chr (1) sys. stdout. write (chr (1) print ''for I in range (1, 11): for j in range (1, I): sys. stdout. write (chr (219) sys. stdout. write (chr (219) print''

 

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.