1.重复元素
描述:编写一个函数,接受一个列表作为参数,返回列表中所有重复的元素。
words = input().split()
results = []
count = 0
for word in words:
if words.count(word) > 1 and word not in results and word !=' ':
results.append(word)
count += 1
for i in range (0,count):
if i != count-1:
print(results[i],end = ' ')
else :
print(results[i])
2.昙花一现
描述:实现一个函数,接受一个字符串作为参数,返回字符串中只出现一次的字符,按照出现的顺序返回。
words = input()
results = []
count = 0
for word in words:
if words.count(word) == 1 and word not in results:
results.append(word)
count += 1
for i in range (0,count):
if i != count-1:
print(results[i],end = ' ')
else :
print(results[i])
3.门禁系统
描述:涛涛最近要负责图书馆的管理工作,需要记录下每天读者的到访情况。
每位读者有一个唯一编号,每条记录用读者的编号来表示。
给出读者的来访记录,请问每一条记录中的读者是第几次出现。
数据范围
1≤𝑛≤1000,
读者的编号为不超过𝑛的正整数。
a = int(input())
lst = list(map(int,input().split(" ")))
num = [1 for x in range(0,a)]
count = [1 for x in range(0,1000)]
for i in range(0,a):
num[i] = count[lst[i]]
count[lst[i]] += 1
for i in num:
print(i,end = " ")
4.
求素数
描述:输入一个数,输出小于等于给定数值的所有素数。
import math
def sushu( x ):
if x < 2:
return False
else:
for i in range(2,int(math.sqrt(x))+1):
if x%i == 0:
return False
return True
a = int(input())
num = [1 for x in range(0,a)]
count = 0
for i in range(2,a+1):
if sushu(i):
num[count] = i
count+=1
for i in range(0,count):
print(num[i],end = " ")
5.递增序列
while True:
a=int(input())
if a==0:
break
else:
for i in range(1,a+1):
if i != a:
print(i,end = ' ')
else:
print(i)
6.排列
描述:给定一个整数n,将数字1∼n排成一排,将会有很多种排列方法。
现在,请你按照字典序将所有的排列方法输出。
n = int(input())
path = [0 for i in range(n)]
tool = [False for i in range(n)]
def dfs( x ):
if x == n:
for i in range(n):
if i != n-1:
print(path[i]+1,end = ' ')
else:
print(path[i]+1)
else:
for i in range(0,n):
if not tool[i]:
path[x] = i
tool[i] = True
dfs(x+1)
tool[i] = False
path[x] = 0
dfs(0)