刷题笔记:
1005 继续(3n+1)猜想
def cover(n):
a = []
while 1:
if n ==1:
a.append(1)
break
else:
if(n%2 == 0):
n = n//2
else:
n = (n*3+1)//2
a.append(n)
return set(a)
n = int(input())
while n:
str1 = input().split(' ')
list1 = set([int(m) for m in str1])
for m in list1:
covers = cover(m)
list1 = list1.difference(covers)
n-=1
list1 = list(list1)
list1.sort(reverse = True)
list1 = [str(b) for b in list1]
print(' '.join(list1))
1)set() 函数创建一个无序不重复元素集。
2)Python 集合 difference() 方法:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.difference(y)
print(z)
#输出{'cherry', 'banana'}
3)
list1.sort(reverse = True)
倒序输出,默认是False(顺序)。
4)
list1 = [str(b) for b in list1]
对列表进行整型或者字符串类型转换需在中括号里面加一个for循环。
5)
思路:写一个覆盖函数返回每个的正整数所对应的覆盖列表。
将K个正整数也存进一个列表list1里,将list1与每个覆盖列表进行difference操作,返回的list1里面的元素就是唯一的,还要将list1的每个元素转变为字符串类型,再用空格隔开输出即可。
1046 划拳
n = int(input())
list1 = []
b = 0
c = 0
for i in range(n):
a = input().split()
list1.append(a)
for i in range(len(list1)):
list2=[]
for j in list1[i]:
list2.append(int(j))
if(list2[0]+list2[2] == list2[1] and list2[0]+list2[2] == list2[3]):
continue
elif(list2[0]+list2[2] != list2[1] and list2[0]+list2[2] != list2[3]):
continue
elif(list2[0]+list2[2] == list2[1] and list2[0]+list2[2] != list2[3]):
b+=1
elif(list2[0]+list2[2] != list2[1] and list2[0]+list2[2] == list2[3]):
c+=1
print("{} {}".format(c,b))
思路:把每次划拳的数据放进列表1,对列表1的每个元素进行操作,对比,平局就continue,然后用b,c分别记录双方赢的次数就行了。
1017 A除以B
a = input().split()
b = int(a[0])//int(a[1])
c = int(a[0])%int(a[1])
print("{} {}".format(b,c))
这题用python会很简单,因为python不需要考虑变量是long还是int等等,系统已经给我们分配好了空间,直接除法取整和做取余计算即可。