Love丶FFC's Blog

CCF-CSP_20150302:数字排序

2019-11-07 12:46:00
阅读:604   •   评论:6
标签:,

冒泡排序

问题描述

给定n个整数,请统计出每个整数出现的次数,按出现次数从多到少的顺序输出。

输入格式

输入的第一行包含一个整数n,表示给定数字的个数。

第二行包含n个整数,相邻的整数之间用一个空格分隔,表示所给定的整数。

输出格式

输出多行,每行包含两个整数,分别表示一个给定的整数和它出现的次数。按出现次数递减的顺序输出。如果两个整数出现的次数一样多,则先输出值较小的,然后输出值较大的。

样例输入

12
5 2 3 3 1 3 4 2 5 2 3 5

样例输出

3 4
2 3
5 3
1 1
4 1

评测用例规模与约定

1 ≤ n ≤ 1000,给出的数都是不超过1000的非负整数。

编程语言:Python

解题思想:

1.首先重开一个列表来存放去除重复后的初始列表。

2.然后判断计数,按照题目要求进行排序并按格式输出。

时间复杂度:O(n^2)

IDE代码如下:

  1. N = int(input()) # 数字的个数
  2. Number = [0 for i in range(N)] # 定义存放数字的列表
  3. Number = [int(n) for n in input().split()] # 将一行的值存入列表
  4. NewNumber = list(set(Number)) # 去除重复后在转为列表存放到另外一个列表中
  5. NewNumberCount = [NewNumber, [0 for i in range(len(NewNumber))]] # 存放去除重复后每个元素的值和计数
  6. # 例如 NewNumber =[1,2,3,4,5] 则NewNumberCount = [[1,2,3,4,5],[0,0,0,0,0]]
  7.  
  8. for i in range(N): # 遍历Number列表
  9. # 无需进行任何判断,因为Number列表中的每个元素都必定存在于NewNumber和NewNumberCount中
  10. NewNumberCount[1][NewNumber.index(Number[i])] += 1
  11. # NewNumber.index(Number[i])用来获得Number[i]在NewNumber中的下标
  12. # NewNumberCount[1][NewNumber.index(Number[i])]用来获得对应的计数
  13.  
  14.  
  15. # 开始按照题目要求进行冒泡排序
  16. def Sort(): # 元素排序
  17. swap = NewNumberCount[1][j]
  18. NewNumberCount[1][j] = NewNumberCount[1][i]
  19. NewNumberCount[1][i] = swap
  20.  
  21. def CountSort(): # 计数排序
  22. swap = NewNumberCount[0][j]
  23. NewNumberCount[0][j] = NewNumberCount[0][i]
  24. NewNumberCount[0][i] = swap
  25.  
  26.  
  27. for i in range(len(NewNumber)):
  28. for j in range(i + 1, len(NewNumber)):
  29. if NewNumberCount[1][j] > NewNumberCount[1][i]:
  30. Sort()
  31. CountSort()
  32. elif NewNumberCount[1][j] == NewNumberCount[1][i]:
  33. if NewNumberCount[0][j] < NewNumberCount[0][i]:
  34. Sort()
  35. CountSort()
  36.  
  37. #按照题目格式输出
  38. for i in range(len(NewNumber)):
  39. print(str(NewNumberCount[0][i])+" "+str(NewNumberCount[1][i]))
  40.  

评论板

共有 6 条评论

  1. fruinly

    Other molecular treatments target the normal human p53 gene, which is a well known tumor suppressor gene buy liquid cialis online

  2. engarma

    TamoxifГЁne Mylan is unlikely to impair the ability of patients to drive or operate machinery generic cialis cost Molecular signature of smoking in human lung tissues

  3. Preliaf

    cialis price Qingjiao was not afraid at all, and the powerful spiritual power burst out instantly, colliding with Huo Wudi

  4. Preliaf

    fucked upp blowejob brerast curvy kayhy barry porn lubed asss tteen anal fuck vids free native aerican indian xxx galleries buying cialis online safely

  5. best yeezy replica

    |An easy way to keep hair off your neck and shoulders is to do a casual up-do. Long hair can sometimes be a hassle. If you have no time to spend with your hair, get an elastic hairband and pull your hair up into a bun. Even if it is a little messy, it will look cute.

  6. https://images.google.com.bz/url?q=https://www.topoakleysoutlet.com/

    |One thing you are going to want to do is always keep an eye open for changes in style. Styles are constantly changing, and you can find out what is new by looking at fashion magazines every now and then. They are likely going to showcase the new trends first.

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:510338