1.翻转
代码
输入数据,每组数据进行比较,j的范围掐头去尾,若a[j]==b[j],继续,若出现010,101子串则改成000,111,遍历完后比较a是否等于b,相同则输出次数,不同则输出-1。
for _ in range(int(input())):a = list(input())b = list(input())cnt = 0for j in range(1,len(a)-1):if a[j] == b[j]:continueelif b[j-1]==b[j+1] and b[j] != b[j-1]:b[j]=b[j-1]cnt += 1print(cnt if a==b else -1)
2.取模
暴力:(只能通过90%)
def f(n,m)->bool:for y in range(1,m+1):for x in range(1,y):if n%x == n%y:return Truereturn Falset = int(input())
for _ in range(t):a,b = map(int,input().split())print('Yes' if f(a,b) else 'No')
抽屉原理:
for _ in range(int(input())):chk=0n,m=map(int,input().split())for i in range(m,1,-1):if(n%i != (i-1)):chk=1breakprint("Yes") if chk else print("No")