close
Content
如果把數字0到9分配成2個整數(各五位數),現在請你寫一支程式找出所有的配對使得第一個數可以整除第二個數,而且商為N(2<=N<=79),也就是:
abcde / fghijk = N
這裡每個英文字母代表不同的數字,第一個數字可以為0。
Input
輸入包含許多筆待測資料,每列代表一筆待測資料,每筆待測資料包含一個正整數N,N為0時代表輸入結束。
Output
對每筆待測資料由小到大輸出每一對符合條件的數。如果找不到符合條件的數對,則輸出There are no solutions for N.。
每筆測試資料間請空一列。
Sample Input #1
61 62 0
Sample Output #1
There are no solutions for 61. 79546 / 01283 = 62 94736 / 01528 = 62
高中生城市可以過,但dome judge過不了
python:
def judge(a,i): st=[0]*10 if (a < 10000 and i < 10000):#題目要求五位數相除又相除是整數所以前面一定不能是四位數後面有時可為四位數 return False if (a < 10000 or i < 10000):#去掉數字重複 st[0] = 1 while (a > 0): if (st[a % 10] == 1): return False else: st[a % 10] = 1 a //= 10 while (i > 0): if (st[i % 10] == 1): return False else: st[i % 10] = 1 i //= 10 return True while 1: n=int(input()) print() ans=0 if n==0: break for i in range(1000,49877):#因為最少四位樹關係從1000開始暴力 a=i*n#取得解 if a>99999:#上限是五位數 continue else: if (judge(a,i)):#判斷解答是否正確正確往下印 ans=1 if len(str(a)) and len(str(i))>3:#最低是四位數我只印四位數的解 if len(str(a))<5:#原始字串小於五個就補零 print('0'+str(a),end=' / ') else: print(a,end=' / ') if len(str(i))<5:#原始字串小於五個就補零 print('0'+str(i),end=' = ') else: print(i,end=' = ')#是五位數正常印 print(n) continue if ans!=1:#表示無解 print('There are no solutions for %d.'%n)
文章標籤
全站熱搜