有培训做网站 小程序的学校,苏州seo整站优化,新手做网站起步教程,c语言入门自学一、Python 一班
1. 基数分割列表
1.1 问题描述
给定一无序数列#xff0c;把数列的第一个数字当成基数#xff0c;让数列中基数小的数字排在数列前面#xff0c;比基数大的数字排在数列的后面。
1.2 问题示例
如数列#xff1a;num[4,1,8,3,9,2,10,7]。基数为 4…一、Python 一班
1. 基数分割列表
1.1 问题描述
给定一无序数列把数列的第一个数字当成基数让数列中基数小的数字排在数列前面比基数大的数字排在数列的后面。
1.2 问题示例
如数列num[4,1,8,3,9,2,10,7]。基数为 4排序后 num[1,3,2,4,8,9,10,7]。
1.3 问题提示
可以使用左右指针方案。
1.4 编码实现
num [4, 1, 8, 3, 9, 2, 10, 7]
# 基数
base_num num[0]
# 左指针
left 0
# 右指针
right len(num) - 1
while left right:while num[right] base_num and right left:right - 1while num[left] base_num and right left:left 1# 交换num[left], num[right] num[right], num[left]
print(num)2. 回旋镖的数量
2.1 问题描述
在平面中给定n个点每一对点都是不同的回旋镖是点的元组ijk其中点i和点j之间的距离与点i和点k之间的距离相同ijk的顺序不同为不同元组。找到回旋镖的数量。n最多为500并且点的坐标都在[-10 00010 000]范围内。
2.2 问题示例
输入[[00][10][20]]输出2两个回旋镖是[[10][00][20]]和[[10][20][00]]。
2.3 问题提示
关键是求解出两个点之间的距离。
2.4 编码实现
def getDistance(a,b):dx a[0] - b[0]dy a[1] - b[1]return dx * dx dy * dydef number0fBoomerangs(points):# 参数points:整数列表#返回整数if points None:return 0ans 0for i in range(len(points)):disCount {}for j in range(len(points)):if i j:continuedistance getDistance(points[i],points[j])count disCount.get(distance,0)disCount[distance] count 1for distance in disCount:ans disCount[distance] * (disCount[distance] - 1)return ansn [[0,0],[1,0],[2, 0]]
print(输入:,n)
print(输出:,number0fBoomerangs(n))3. 合并排序数组
3.1 问题描述
合并两个排序的整数数组A和B变成一个新的排序数组。
3.2 问题示例
输入[123]及元素个数3输入[45]及元素个数2输出[12345]经过合并新的数组为[12345]。输入[125]及元素个数3输入[34]及元素个数2输出[12345]经过合并新的数组为[12345]。
3.3 编码实现
a [1, 4, 6]
b [3, 5]
c []
i 0
j 0
while i len(a) and j len(b):if a[i] b[j]:c.append(a[i])i 1else:c.append(b[j])j 1
while i len(a):c.append(a[i])i 1
while j len(b):c.append(b[j])j 1
print(c)二、Python 二班
1. 构造矩形
1.1 问题描述
给定一个矩形大小设计其长L宽W使其满足如下要求
矩形区域大小需要和给定目标相等宽度W不大于长度L即L≥W长和宽的差异尽可能小返回设计好的长度L和宽度W。
1.2 问题示例
输入为4输出为[22]目标面积为4所有可能的组合有[14][22][41][22]是最优的L2W2。
1.3 问题提示
给定区域面积不超过10 000 000而且是正整数页面宽度和长度必须是正整数。
1.4 编码实现
方案一两头向中思路求差值最小的一对长和宽
area int(input(矩形大小))
wi 1
le area
c le-wi
for i in range(1, area 1):le area // iif le - i c and le i and le * i area:wi ic le - i
print(int(area / wi), wi)方案二中间开始试探查找 。
import math
area int(input(矩形大小))
w int(math.sqrt(area))
while area % w ! 0:w - 1
print(area // w, w)2. 两个排序数组合的第k小元素
2.1 问题描述
给定两个排好序的数组AB定义集合sumab其中a来自数组Ab来自数组B求sum中第k小的元素。
2.2 问题示例
给出A[1711]B[246]sum[35791113131517]当k3返回7当k4返回9当k8返回15。
2.3 问题提示
最简单的方案2 个列表中的数字相乘组成新的列表再排序。
2.4 编码实现
k int(input(位置))
A [1, 7, 11]
B [2, 4, 6]
s []
for i in range(len(A)):for j in range(len(B)):s.append(A[i] B[j])
s.sort()
print(s[k - 1])三、Python 三班
1. 首字母大写
1.1 问题描述
输入一个英文句子将每个单词的首字母改成大写
1.2 问题示例
输入si want to go home输出I Want To Go Home。输入swe want to go to school输出We Want To Go To School。
1.3 问题提示
可以使用 split分割字符串取出首字母后转换成大写(可以使用字符串的 upper()方法把字母转成大写)。
1.4 编码实现
s i want to go home
lst s.split( )
print(lst)
for i in range(len(lst)):lst[i] lst[i][0].upper()lst[i][1:]
print( .join(lst))2. 七进制
2.1 问题描述
给定一个整数返回其七进制的字符串表示。
2.2 问题示例
输入num100输出202。输入num7输出10。
2.3 问题提示
求解七进制方法把数字不停除以 7。再把余数重新连接起来。
如求解 100 的 七进制 100 除以 7 余数为 2商为 14. 14 再除以 7余数为 0,商为 2。 2 再除了 7 余数为 2商为 0。 从后向前把余数连接起来202。
2.4 编码实现
num7
res
while num0:resstr(num % 7)numnum // 7
print(res[::-1])3. 查找数组中没有出现的所有数字
3.1 问题描述
给定一个整数数组其中1≤a[i]≤nn为数组的大小一些元素出现两次其他元素出现一次。找到[1n]中所有未出现在此数组中的元素。
3.2 问题示例
输入[43278231]输出[56]。
3.3 问题提示
逐一查找
3.4 编码实现
num [4, 3, 2, 7, 8, 2, 3, 1]
for i in range(1,len(num)1):if i not in num:print(i)
文章转载自: http://www.morning.kpxnz.cn.gov.cn.kpxnz.cn http://www.morning.qqbjt.cn.gov.cn.qqbjt.cn http://www.morning.yfphk.cn.gov.cn.yfphk.cn http://www.morning.jrbyz.cn.gov.cn.jrbyz.cn http://www.morning.ggtkk.cn.gov.cn.ggtkk.cn http://www.morning.wcrcy.cn.gov.cn.wcrcy.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.nzsdr.cn.gov.cn.nzsdr.cn http://www.morning.cgthq.cn.gov.cn.cgthq.cn http://www.morning.zpdjh.cn.gov.cn.zpdjh.cn http://www.morning.nmkbl.cn.gov.cn.nmkbl.cn http://www.morning.mpyry.cn.gov.cn.mpyry.cn http://www.morning.wslr.cn.gov.cn.wslr.cn http://www.morning.yqlrq.cn.gov.cn.yqlrq.cn http://www.morning.gcszn.cn.gov.cn.gcszn.cn http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn http://www.morning.xbbrh.cn.gov.cn.xbbrh.cn http://www.morning.gbybx.cn.gov.cn.gbybx.cn http://www.morning.rqqkc.cn.gov.cn.rqqkc.cn http://www.morning.bbjw.cn.gov.cn.bbjw.cn http://www.morning.cfnht.cn.gov.cn.cfnht.cn http://www.morning.nqlnd.cn.gov.cn.nqlnd.cn http://www.morning.xcszl.cn.gov.cn.xcszl.cn http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.mqzcn.cn.gov.cn.mqzcn.cn http://www.morning.bxczt.cn.gov.cn.bxczt.cn http://www.morning.tkflb.cn.gov.cn.tkflb.cn http://www.morning.xxrgt.cn.gov.cn.xxrgt.cn http://www.morning.qkqgj.cn.gov.cn.qkqgj.cn http://www.morning.srbbh.cn.gov.cn.srbbh.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.rnnq.cn.gov.cn.rnnq.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.ckwxs.cn.gov.cn.ckwxs.cn http://www.morning.tqwcm.cn.gov.cn.tqwcm.cn http://www.morning.jzkqg.cn.gov.cn.jzkqg.cn http://www.morning.tfkqc.cn.gov.cn.tfkqc.cn http://www.morning.xmyrn.cn.gov.cn.xmyrn.cn http://www.morning.xlxmy.cn.gov.cn.xlxmy.cn http://www.morning.qhvah.cn.gov.cn.qhvah.cn http://www.morning.qwnqt.cn.gov.cn.qwnqt.cn http://www.morning.tqpnf.cn.gov.cn.tqpnf.cn http://www.morning.qctsd.cn.gov.cn.qctsd.cn http://www.morning.ycgrl.cn.gov.cn.ycgrl.cn http://www.morning.nmngg.cn.gov.cn.nmngg.cn http://www.morning.rjrlx.cn.gov.cn.rjrlx.cn http://www.morning.qlpyn.cn.gov.cn.qlpyn.cn http://www.morning.gmnmh.cn.gov.cn.gmnmh.cn http://www.morning.rwmq.cn.gov.cn.rwmq.cn http://www.morning.dxtxk.cn.gov.cn.dxtxk.cn http://www.morning.xxrwp.cn.gov.cn.xxrwp.cn http://www.morning.gtbjc.cn.gov.cn.gtbjc.cn http://www.morning.kwrzg.cn.gov.cn.kwrzg.cn http://www.morning.ftlgy.cn.gov.cn.ftlgy.cn http://www.morning.swkpq.cn.gov.cn.swkpq.cn http://www.morning.wwthz.cn.gov.cn.wwthz.cn http://www.morning.zcfmb.cn.gov.cn.zcfmb.cn http://www.morning.rbffj.cn.gov.cn.rbffj.cn http://www.morning.pslzp.cn.gov.cn.pslzp.cn http://www.morning.clbzy.cn.gov.cn.clbzy.cn http://www.morning.tnyanzou.com.gov.cn.tnyanzou.com http://www.morning.wjhnx.cn.gov.cn.wjhnx.cn http://www.morning.nypsz.cn.gov.cn.nypsz.cn http://www.morning.ftcrt.cn.gov.cn.ftcrt.cn http://www.morning.htqrh.cn.gov.cn.htqrh.cn http://www.morning.xhhzn.cn.gov.cn.xhhzn.cn http://www.morning.qrgfw.cn.gov.cn.qrgfw.cn http://www.morning.qwlml.cn.gov.cn.qwlml.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.fkmrj.cn.gov.cn.fkmrj.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.pkggl.cn.gov.cn.pkggl.cn http://www.morning.gxcit.com.gov.cn.gxcit.com http://www.morning.qggxt.cn.gov.cn.qggxt.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.zrdhd.cn.gov.cn.zrdhd.cn http://www.morning.lhldx.cn.gov.cn.lhldx.cn