大鹏

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

 

 

 

 1 while True:
 2     try:
 3         num = int(input())
 4         stack = []
 5         for i in range(num):
 6             stack.append(input())
 7         print("\n".join(sorted(stack)))
 8     except:
 9         break
10 
11 
12 ##############################################################3
13 n = int(input().strip())  # 先处理输入
14 strlist = []
15 while n > 0:
16     istr = input()
17     strlist.append(istr)
18     n -= 1 #输入的次数
19 
20 # 定义排序函数,返回是否交换顺序
21 def strOrder(a, b):
22     la = len(a)
23     lb = len(b)
24     for i in range(min(la, lb)):
25         if ord(a[i]) < ord(b[i]):
26             return 0  #
27         elif ord(a[i]) > ord(b[i]):
28             return 1
29         else:
30             continue
31     if la <= lb:
32         return 0
33     else:
34         return 1
35 
36 
37 # 冒泡排序
38 for i in range(len(strlist) - 1):
39     for j in range(i + 1, len(strlist)):
40         if strOrder(strlist[i], strlist[j]):  # 满足条件,则交换二者顺序
41             tmp = strlist[i]
42             strlist[i] = strlist[j]
43             strlist[j] = tmp
44 for i in range(len(strlist)):
45     print(strlist[i])
View Code

 

 1 n = int(input())
 2 while True:
 3     try:
 4         lis = []
 5         for i in range(n):
 6             sr = input()
 7             lis.append(sr)
 8         lis.sort()
 9         print('\n'.join(lis))
10     except (EOFError, KeyboardInterrupt):
11         break
View Code

 

2\

 

 

 

 1 count+1,然后n除2取整后继续,由于除了0以外的n的二进制都至少有一个1 故count从1开始
 2 n = int(input())
 3 count = 1
 4 while n != 1:
 5     k = n % 2
 6     if k == 1:
 7         count +=1
 8     n = n // 2
 9 print(count)
10 
11 
12 import sys
13 line=int (sys.stdin.readline().strip())
14 res=0
15 while line>0 :
16     if line%2 ==1 :
17         res+=1
18     line=line//2
19 print res
20 
21 num = int(input())
22 counts = 0
23 while num !=0:
24     if num % 2 == 1:
25         counts = counts+1
26     num = num >> 1
27 print(counts)
28 
29 n = int(input())
30 count = 1
31 while n != 1:
32     k = n % 2
33     if k == 1:
34         count +=1
35     n = n // 2
36 print(count)
37 
38 
39 
40 #先转换为二进制,再转换为字符串,计算字符串中的‘1’的数量
41 intNum = int(input())
42 byteNum = bin(intNum)
43 byteStr = str(byteNum)
44 count = 0
45 for i in byteStr:
46     if i == '1':
47         count += 1
48 print(count)
49 
50 
51 num = int(input())
52 bin_num_ss = bin(num)
53 ct =0
54 for i in range(len(bin_num_ss)-1,1,-1):
55     if bin_num_ss[i] == '1':
56         ct +=1
57 print(ct)  
58 
59 
60 print(bin(int(input())).count("1"))
61 #bin()返回一个整数 int 或者长整数 long int 的二进制表示。返回类型为字符串
62 #count()方法用于统计字符串里某个字符出现的次数
View Code

 

 

 

 

 

 

 

 

3\

 

 

 

 

 

 

4\

 

 

 

 

 

 

 

 

5\

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1

 

posted on 2022-03-31 20:42  pf42280  阅读(37)  评论(0编辑  收藏  举报