python计算题_Python练习题
首先废话几句,因为最近学了Python的一些基础的语法,但是发现看书看的不少但是练习做的太少。要学好一门语言最重要的是要多敲代码,在练习中是可以发现很多的问题,这些都是单纯的看书无法得到的。所以鉴于网上关于Python练习题的资源相对较少,而且有的资源只是给出了问题但是没有提供参考的代码。于是我斗胆作为一个小白整理了一系列的Python练习题,希望能给同样的志同道合的Python热爱者提供一些方便。(作为一个刚入道不久的小白,文章中难免有些糟粕,代码也难免有些麻烦或者错误的地方,希望看见的有缘人在下方的评论中可以指出来,在下感激不尽!)
Python练习题:
1.有 1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多 少?
1 #encoding=utf-8
2 __author__ = 'heng'
3 #利用1,2,3,4可以组成多少个三位数,并且没有重复数字
4 figure = [1,2,3,4]5 number =06 for x infigure:7 for y infigure:8 if x ==y:9 continue
10 else:11 for z infigure:12 if y == z or z == x: #注意是or不是and
13 continue
14 else:15 number += 1
16 print 100*x + 10*y +z17 print "the number is %s"%number
2.企业发放的奖金根据利润提成。利润(I): 低于或等于10万元时,奖金可提10%; 高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时,高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%, 高于100万元时, 超过100万元的部分按1%提成, 从键盘输入当月利润I,求应发放奖金总数?
1 #encoding=utf-8
2 __author__ = 'heng'
3 #题目不再累述
4 the_profit = float(raw_input("please enter the profit:"))5 money_award = 0.0
6 if the_profit <= 10:7 money_award = the_profit * 0.1
8 elif the_profit <= 20:9 money_award = 10 * 0.1 + (the_profit-10)*0.075
10 elif the_profit <=40:11 money_award = 10*0.1 + 10*0.075 + (the_profit-20)*0.05
12 elif the_profit <= 60:13 money_award = 10*0.1 + 10*0.075 + 20 * 0.05 + (the_profit-40)*0.03
14 elif the_profit <= 100:15 money_award = 10*0.1 + 10*0.075 + 20 * 0.05 + 20*0.03 + (the_profit-60)*0.015
16 elif the_profit > 100:17 money_award = 10*0.1 + 10*0.075 + 20 * 0.05 + 20*0.03 + 40 * 0.015 + (the_profit - 100) * 0.01
18 print "the money award is:%s"%money_award
貌似感觉这个的代码量确实比较大但是时间复杂度还是相对比较低的
3.一个整数,它加上 100后是一个完全平方数,再加上168又是一个完全平方数, 请问该数是多少?
针对这个问题,我暂时只是想到了最笨最直接的方法,如果还有更加简便的方法希望大神可以在下面评论中告诉我。(感激不尽)
1 #encoding=utf-8
2 __author__ = 'heng'
3 #找出符合题目要求的数字
4 from math importsqrt5 the_figure =06 whileTrue:7 if sqrt(the_figure + 100) == int(sqrt(the_figure+100)):8 if sqrt(the_figure +268) == int(sqrt(the_figure+268)):9 print "the figure is:%s"%the_figure10
11 the_figure += 1
4.输入某年某月某日,判断这一天是这一年的第几天?
在这里提供两种方法显示出Python中字典的优越性!(我竟然开始没有想到用字典)
(1)笨方法:
1 #encoding=utf-8
2 __author__ = 'heng'
3 #输入一个时期判断是当年的第几天
4
5 def ifleapyear(the_year): #判断是不是闰年
6 if the_year % 4 == 0 and the_year % 100 != 0 or the_year %400 ==0:7 returnTrue8 else:9 returnFalse10
11 #主程序
12 year = int(raw_input("please enter the year:"))13 month = int(raw_input("please enter the month:"))14 days = int(raw_input("please enter the days"))15 the_number_day =016 for i in range(1,month):17 if i ==1:18 the_number_day +=31
19 if i == 2:20 ififleapyear(year):21 the_number_day += 29
22 else:23 the_number_day += 28
24 if i ==3:25 the_number_day += 31
26 if i==4:27 the_number_day += 30
28 if i ==5:29 the_number_day += 31
30 if i==6:31 the_number_day +=30
32 if i ==7:33 the_number_day +=31
34 if i ==8:35 the_number_day +=31
36 if i==9:37 the_number_day += 30
38 if i ==10:39 the_number_day += 31
40 if i ==11:41 the_number_day +=30
42 if i==12:43 the_number_day +=31
44 the_number_day +=days45 print("the number of the days is %s"%the_number_day)
(2)利用字典(竟然是如此的简单)
1 #encoding=utf-8
2 __author__ = 'heng'
3 #运用字典的优越性来解决
4 #判断是不是闰年
5 defifleapyear(the_year):6 if the_year % 4 == 0 and the_year % 100 != 0 or the_year %400 ==0:7 returnTrue8 else:9 returnFalse10
11 #主程序
12 year = int(raw_input("please enter the year:"))13 month = int(raw_input("please enter the month:"))14 days = int(raw_input("please enter the days"))15 the_number_day =016 the_month = {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}17 ififleapyear(year):18 the_month[2] = 29
19 if month == 1:20 pass
21 else:22 for i inthe_month.keys():23 if i==month:24 break
25 the_number_day +=the_month[i]26 the_number_day +=days27 print("the number of day is %s"%the_number_day)
字典的优越性显而易见啊!
4.输入x,y,z三个整数,将这三个整数按从大到小的顺序输出
1 #encoding=utf-8
2 __author__ = 'heng'
3 #输入x,y,z 三个数,然后按照从小到大的顺序排列出来
4 x = float(raw_input("please enter x:"))5 y = float(raw_input("please enter y:"))6 z = float(raw_input("please enter z:"))7 lis =[x,y,z]8 lis.sort()9 print lis
5.利用Python输出一个由*组成的C
1 #encoding=utf-8
2 __author__ = 'heng'
3 #利用*输出C的图案
4 print '*'*5
5 print '*'
6 print '*'
7 print '*'*5
6.利用*输出一个矩阵
主要是想通过这个代码熟悉一下方法rjust()
1 #encoding=utf-8
2 __author__ = 'heng'
3 #利用*输出一个(30 X 20)的矩阵
4 l = '*'*30
5 printl6 for w in range(1,19):7 print '*' + '*'.rjust(29)8 print l
7.输出9x9的口诀表
1 #encoding=utf-8
2 __author__ = 'heng'
3 #输出9x9的乘法口诀表
4 for x in range(1,10):5 for y in range(1,x+1):6 print '%sx%s=%s'%(y,x,x*y), #如果想让连续输出的print不换行就在后面加上,
7 print ''
8.计算1000的阶乘
这里给出两种经典的方法,也就是利用for循环解决和利用递归解决。
for循环解决:
1 #for循环实现1000的阶乘
2 the_end = 1
3 for i in range(2,1001):4 the_end = the_end *i5 print the_end
递归解决:
1 #利用递归实现1000的阶乘
2 defprocess(i):3 if i == 1: #这一条一定要加上,要不然会算到负数中造成内存的溢出
4 return 1
5 else:6 return i * process(i-1) #递归函数一定要记的返回值
7 #主程序
8 print process(1000)
两种解决分析:利用递归似乎程序会更简洁些,但是利用递归要注意防止内存的溢出。所以对与类似的问题个人感觉还是利用for循环解决更好
9.利用Python实现四大排序算法
(1)冒泡排序:
所谓冒泡排序法就是用一个数和它前面的所有数做比较,找出其中自己合适的位置
1 the_list = [8,12,45,1,2,45,3,0,5]2 for x inrange(len(the_list)):3 for y inrange(x):4 if the_list[x]
(2)插入排序
插入排序法,就是将相邻的两个数比较,交换之后继续和前面的数比较,满足条件就依次交换,注意不要将插入排序和冒泡排序弄混乱
1 #实现插入排序
2 #定义一个插入排序的函数
3 defthe_insert(the_list):4 for x inrange(len(the_list)):5 i =x6 whilei:7 if the_list[i] < the_list[i-1]:8 t =the_list[i]9 the_list[i] = the_list[i-1]10 the_list[i-1] =t11 i -= 1
12 returnthe_list13
14 #主函数
15 the_list = [3,2,5,1,0]16 print the_insert(the_list)
(3)选择排序
1 #encoding=utf-8
2 __author__ = 'heng'
3 #选择排序
4 #选择排序函数
5 defthe_choice(the_list):6 for i in range(len(the_list)-1):7 x =i8 while x != len(the_list)-1:9 x += 1
10 if the_list[i] >=the_list[x]:11 t =the_list[i]12 the_list[i] =the_list[x]13 the_list[x] =t14 returnthe_list15 #主函数
16 the_list = [7,3,6,1,7,2,9,11,10]17 print the_choice(the_list)
(4)快速排序法
快速排序法用Python的递归会更加的简单明了
1 #encoding=utf-8
2 __author__ = 'heng'
3 #利用递归实现快速排序法
4 #用来排序的递归函数
5 defquick_sort(L,low,high):6 i =low7 j =high8 if i >=j:9 returnL10 key =L[i]11 while i
<<
python计算题_Python练习题相关推荐
- python计算题_python计算题
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 以后我还会分享一些精妙而又有趣的python算法题. 今天也给大家分享几个pyt ...
- 50道简单python函数题_Python练习题 函数设计(55~67)一
Demo55 ** Python 计算机程序设计中会经常用到十六进制数(第3章曾介绍过)(参见附录C中对数系的介 绍).将十进制数d转换为一个十六进制数就是找到满足下面条件的十六进制数: ** 程序编 ...
- 初一上册数学用计算机进行运算,人教版初一数学上册计算题及练习题
人教版初一数学上册计算题及练习题 初一数学上册计算题(400 道题)(1) = (2) = 231-(3) = ( 4) = 9 4(5) = (6) =2031 23 ...
- python 计算时间重叠_Python基于时间信息(即时、间隔)计算项目之间的相似性...
我想根据时间信息计算项目(0,1,2,3-.)之间的相似性.时间信息可以是时间即时(startdate).时间间隔(startdate.enddate)或null(NaT):请参阅下面的datafra ...
- python计算两点间距离_python 计算方位角实例(根据两点的坐标计算)
知道两点坐标,怎么计算两点方向的方位角? 答:首先计算坐标增量dx,dy(两个对应坐标分量相减,终点的减始点的). 若dx,dy中有一个为零时,根据另一个的正负决定方位角(0,90,180,270这四 ...
- python 计算时间重叠_python whilepython计算两个矩形框重合百分比的实例
如下所示: 对<python计算两个矩形框重合百分比的实例>总结来说,为我们电脑技术很实用. def mat_inter(box1,box2): # 判断两个矩形是否相交 # box=(x ...
- python 100题_python 100题
网上搜查到的Python算法题,自己思考完成 [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数(比如121,212,题目应该是无连续重复数字)都是多少? 程序分析:可 ...
- python计算导数_python计算导数并绘图的实例
我就废话不多说了,直接上代码吧! import math import numpy as np import matplotlib.pyplot as plt from sympy import * ...
- python计算圆周率_python模拟蒙特·卡罗法计算圆周率
蒙特·卡罗方法是一种通过概率来得到问题近似解的方法,在很多领域都有重要的应用,其中就包括圆周率近似值的计问题. 假设有一块边长为2的正方形木板,上面画一个单位圆,然后随意往木板上扔飞镖,落点坐标(x, ...
最新文章
- windows服务编程 注册InstallUtil.exe
- 线性代数【8】-1 线性方程组
- phpmyadmin mysql float 长度_mysql – phpMyAdmin – “请输入有效长度”
- tensorflow中命名空间、变量命名的问题
- 【转】90后还过五四吗?这些“脸熟”的过来人送给青年10句忠告
- 反射进行.newInstance()方法,报错Caused by: java.lang.NoSuchMethodException:XXXX.<init>()
- JavaScript 高级程序设计笔记
- javaweb day14
- 易地推拓客分享:内容获客是最稳定的获客方式之一
- python文献翻译_Python实现一键翻译英文论文,实现了pdf文档英译汉,pdf格式不变...
- mysql怎么设置id自动编号_MySQL中实现ID编号自动增加的方法
- 风口下的追逐:AI正在驾驶、客服、教育领域疾驰
- 如何从数据库中选出最热的十个检索词
- UltraVNC 使用,内网局域网远程控制
- 短域名Andy.ge 安迪哥的启用
- 七段数码管的使用(使能端分时控制)
- 服务器raid1硬盘更换,服务器做 Raid1,一块硬盘坏如何更换? - 电脑技巧 Power By www.hugesky.com...
- linux新建分区步骤
- 【改】[火光摇曳]神奇的伽玛函数(上)——markdown排版
- 区块链培训中的Golang为什么这么重要?怎么学?
热门文章
- python 实例方法 类方法_Python实例方法 静态方法 类方法
- theadClasses设置Bootstrap Table表头样式
- 【文文殿下】[BZOJ4008] [HNOI2015] 亚瑟王
- MyBatis学习(十)--MyBatis数据操作
- ICC_lab总结——ICC_lab3:布局
- webservice wsdl 生成服务
- [LeetCode] Merge Sorted Array
- 有一些无声话语,只有寻梦的人彼此听得见
- [转载] python super详解
- [转载] 终于来了!TensorFlow 2.0入门指南(上篇)