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)

我是通过的奥,可不是瞎写的奥:

 

posted @ 2022-04-06 20:58  lures  阅读(166)  评论(0)    收藏  举报