1. 程式人生 > 其它 >L1-027 出租 (20 分) Python

L1-027 出租 (20 分) Python

下面是新浪微博上曾經很火的一張圖:

 

 

一時間網上一片求救聲,急問這個怎麼破。其實這段程式碼很簡單,index陣列就是arr陣列的下標,index[0]=2 對應 arr[2]=1index[1]=0 對應 arr[0]=8index[2]=3 對應 arr[3]=0,以此類推…… 很容易得到電話號碼是18013820100

本題要求你編寫一個程式,為任何一個電話號碼生成這段程式碼 —— 事實上,只要生成最前面兩行就可以了,後面內容是不變的。

輸入格式:

輸入在一行中給出一個由11位數字組成的手機號碼。

輸出格式:

為輸入的號碼生成程式碼的前兩行,其中arr中的數字必須按遞減順序給出。

輸入樣例:

 1 18013820100 

輸出樣例:

1 int[] arr = new int[]{8,3,2,1,0};
2 int[] index = new int[]{3,0,4,3,1,0,2,4,3,4,4};

寫這個部落格是因為搜關鍵詞沒找到python寫的奧,於是寫了個隨便隨便發一下:

 1 tell = input().strip()
 2 
 3 tell_dict = {}
 4 for i in tell:
 5     if i not in tell_dict:
 6         tell_dict[i] = 1
 7 # 獲取鍵構成的列表,按照遞減順序排列
 8 keys = sorted(list(tell_dict.keys()), reverse=True)
9 nums = [] 10 for i in tell: 11 nums.append(keys.index(i)) 12 keys_str = ",".join(keys) 13 print("int[] arr = new int[]{%s};" % keys_str) 14 nums = list(map(str,nums)) 15 nums_str = ",".join(nums) 16 print("int[] index = new int[]{%s};" % nums_str)

我是通過的奧,可不是瞎寫的奧: