# -*- coding: utf-8 -*-"""
Created on Fri Dec 24 19:06:43 2021
Function:
@author: 小梁aixj
"""classSolution(object):defaddBinary(self, a, b):
res =''
lsa, lsb =len(a),len(b)
pos, curr =-1,0while(lsa + pos)>=0or(lsb + pos)>=0:if(lsa + pos)>=0:
curr +=int(a[pos])if(lsb + pos)>=0:
curr +=int(b[pos])
res =str(curr %2)+ res
curr //=2
pos -=1if curr ==1:
res ='1'+ res
return res
# %%
s = Solution()print(s.addBinary(a ='1010', b ='1011'))#10101
3,运行结果
二、输出整数的全排列
1,程序简介
输入整数n(3<=n<=7),编写程序输出1,2,…,n整数的全排列,按字典序输出。
输入样例:
输入:3
输出:123 132 213 231 312 321
2,程序代码
# -*- coding: utf-8 -*-"""
Created on Fri Dec 24 19:07:39 2021
Function:
@author: 小梁aixj
"""import random
count=0
n =int(input('输入整数n(3<=n<=7):'))
t =list()
t1 =set()for i inrange(1, n+1):
t.append(str(i))whileTrue:sum=1for i inrange(1, n +1):sum*= i
iflen(t1)>=sum:break
random.shuffle(t)
t1.add(''.join(t))
s =sorted(t1)for i in s:print(i,end =' ')
count = count+1if count%4==0:print()