猴子第一天摘下若干个套装,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天找事都吃了前一天剩下的一半零一个。到第10天找事想吃时,只剩下一个桃子了,求第一天共摘多少个桃子
num = 1 #第10天剩下1个桃子
for i in range(9,0,-1):
num = (num+1)*2
print("第{}天有{}个".format(i,num))
print(num)
#求杨辉三角第m行的第k个元素,
#1.第m行有m项,m是正整数,因此k一定不会大于m
#2.第n行的m个元素可表示为C(n-1,m-1),即为从n-1个不通元素中取m-1个元素的组合数
n = int(input("请输入要打印的行数:"))
arr = [1]*(n+1)
nn = n//2 if n&1 else n//2 +1
for i in range(1,nn+1):
num1 = 1
num2 = 1
for k in range(1,i+1):
num1 *=(n-k+1)
num2 *= k
num3 = int(num1/num2)
arr[i]= num3
arr[-i-1]= num3
print(arr);
#随机产生10个数,每个数字取值范围是[1,20],统计重复的数字有几个?分别是什么?不重复的数字有几个?分别是什么
import random
str1 = []
str2 = []
for i in range(10):
num = random.randint(1,20)
if num in str2:
continue
if num in str1:
str2.append(str1.pop(str1.index(num)))
else:
str1.append(num)
print("有{}个数重复,分别是:{}".format(len(str2),str2))
print("有{}个数没有重复过,分别是:{}".format(len(str1),str1))
#第二种解法
n = 20
num = [ random.randint(1,20) for _ in range(n)]
numindex = [0]*n
retetition = [] #二维数组,重复的数字和次数
noretetition = [] #不重复的数字
for i in range(len(num)):
if numindex[i] != 0: continue
count = 0
for k in range(i+1,len(num)):
if numindex[i] != 0: continue
if num[k] == num[i]:
numindex[k] = 1
count +=1
if count:
retetition.append([num[i],count+1])
else:
noretetition.append(num[i])
print(num)
print("重复:",retetition)
print("不重复:",noretetition)
##第一题 用户输入数字统计
#用户输入一个数字,打印每一位数字及其重复的次数
import random
#arr = [ random.randint(0,9) for _ in range(20)]
arr = [int(input("请输入一个数字")) for _ in range(10)]
#arr = [1,2,3,5,1,2]
countdict = {};
for i in arr:
countdict[i] = countdict.setdefault(i,0) + 1
for k,v in countdict.items():
print("{}出现了{}次".format(k,v))
print(countdict)
#随机产生100个整数,数字范围[-1000,1000]升序输出这些数字并打印其重复的次数
####第一种思路,排序后统计
import random
from collections import OrderedDict
arr = [random.randint(-1000,1000) for _ in range(100)]
arr.sort()
odict = OrderedDict()
for i in range(len(arr)):
odict[arr[i]] = odict.setdefault(arr[i],0)+1
if i+1<len(arr) and arr[i]!=arr[i+1]:
print("{}出现了{}次".format(arr[i],odict[arr[i]]))
print("{}出现了{}次".format(arr[-1],odict[arr[-1]]))
#print(arr,odict,sep="\n")
#####第二种思路,统计后,排序keys,然后输出
import random
#arr = [1,2,3,5,4,2,3,1]
arr = [random.randint(-1000,1000) for _ in range(100)]
countdict = {}
for i in arr:
countdict[i] = countdict.setdefault(i,0) + 1
countkey = list(countdict.keys())
countkey.sort()
for i in countkey:
print("{}出现了{}次".format(i,countdict[i]))
#print(arr,countdict,countkey,sep="\n")
##第三题 字符串重复统计 字符表’a~z’随机挑选2个字母组成字符串,共挑选100次,降序输出这100个字符串及重复的次数
import random,string
arr = [ "".join(random.choices(string.ascii_lowercase,k=2)) for _ in range(100)]
countdict = {}
for i in arr:
countdict[i] = countdict.setdefault(i,0)+1
sortkey = list(countdict.keys())
sortkey.sort()
for i in sortkey:
print("{}出现了{}次".format(i,countdict[i]))
#打印九九乘法表
arr = [ print("{}*{}={}".format(k,i,i*k),end= "\n" if i==k else "\t") for i in range(1,10) for k in range(1,i+1)]
##"0001.abadicddws"是Id格式,要求ID格式是以点号分割,左边是4位从1开始的整数,右边是10位随机小写英文字母。请依次生成前100个Id的列表
["{:0>4}.{}".format(i,bytes(random.choices(range(97,123),k=10)).decode()) for i in range(1,101)]
#矩阵转换
#显示矩阵
def showMatrix(arr):
for i in arr:
print("\t".join([str(_) for _ in i]))
#创建矩阵
def getMatrix(n):
arr = [[0]*n for _ in range(n)]
count = 1
for i in arr:
for k in range(len(i)):
i[k] = count
count += 1
return arr
#翻转矩阵
def overMatrix(arr):
for i in range(len(arr)):
for k in range(0,i):
arr[i][k],arr[k][i] = arr[k][i],arr[i][k]
return arr
#调用
arr = getMatrix(5)
showMatrix(arr)
print("--------------")
showMatrix(overMatrix(arr))
#翻转不规则矩阵
##创建矩阵
def getMatrix(x,y):
arr = [[0]*x for _ in range(y)]
count = 1
for i in arr:
for k in range(len(i)):
i[k] = count
count += 1
return arr
#显示矩阵
def showMatrix(arr):
for i in arr:
print("\t".join([str(_) for _ in i]))
#翻转矩阵
def overMatrix(arr):
x = len(arr[0])
y = len(arr)
arr1 = [[0]*y for _ in range(x)]
for i in range(len(arr1)):
for k in range(len(arr1[i])):
arr1[i][k] = arr[k][i]
return arr1
#直接翻转显示矩阵
def showOverMatrix(arr):
for i in range(len(arr[0])):
for k in range(len(arr)):
print(arr[k][i],end="\t")
print("")
arr = getMatrix(5,2)
showMatrix(arr)
print("------")
showOverMatrix(arr)
#showMatrix(overMatrix(arr))
matrix = [[1,2,3],[4,5,6]]
tm = []
for i,row in enumerate(matrix):
for k,col in enumerate(row):
# if i==0: #
# tm.append([])
if len(tm) < k +1:
tm.append([])
tm[k].append(col)
# print(col,sep="",end="")
# print("")
print(*tm,sep="\n")