liangxinxin5102 / PAT

Some PAT questions and ancesters
0 stars 0 forks source link

一些实用函数和经典算法问题python3 #4

Open liangxinxin5102 opened 5 years ago

liangxinxin5102 commented 5 years ago

进制转换

def to_str(n,base): convert_string='0123456789ABCDEF' if n<base: return convert_string[n] else: return to_str(n//base,base)+convert_string[n%base] print(to_str(1453,16))

liangxinxin5102 commented 5 years ago

未完成:

八皇后问题

农夫过河问题

找零钱

def conflict(list1,m,n): count=0 for i in range(8): if n!=i and list1[m][i]==1: count=count+1 if m!=i and list1[i][n]==1: count=count+1 for i in range(1,8): if m+i<8 and n+i<8 and list1[m+i][n+i]==1: count=count+1 if m-i>0 and n-i>0 and list1[m-i][n-i]==1: count=count+1 if m+i<8 and n-i>0 and list1[m+i][n-i]==1: count=count+1 if m-i>0 and n+i<8 and list1[m-i][n+i]==1: count=count+1 if count==6: return 0 else: return 1 def main(): list1 = [[0] * 8 for i in range(8)] for m in range(8): for n in range(8): list1[m][n]=1 if conflict(list1,m,n): list1[m][n]=0 break for i in range(8): print(list1[i]) if name == 'main': main() 八皇后问题想到很晚,最后知道哪里错了,冲突函数边上的各自有点特殊,然后还有不知道怎么改,自己没有去百度看解决的代码。但是还是有收获的,自己学会了调试功能,pycharm真的好用。