当前位置:网站首页>TZC 1283: 简单排序 —— 函数法
TZC 1283: 简单排序 —— 函数法
2022-07-18 20:23:00 【橙子教师】
我们以TZC 1283为例简单说明一下排序(含十大经典排序算法)的python实现方法和C实现方法。首先来说函数法,python中可以使用sort函数,C语言可以使用qsort函数实现。
原题链接:1283: 简单排序
Python写起来比较简单,代码如下:
T = int(input())
for i in range(T):
s = input().split()
lt = [int(x) for x in s]
lt1 = lt[::-1]
lt1.pop()
lt1.sort()
n = len(lt1)
for j in range(n):
if j != n-1:
print(lt1[j], end=' ')
else:
print(lt1[j])
C语言不像 C++ 或 python,没有sort函数,只有qsort函数,使用起来没有那么方便,需要自定义比较函数cmp。
#include<stdio.h>
int cmp(const void *a ,const void *b)
{
return *(int *)a - *(int *)b;
}
int main()
{
int m,n,i,j,a[1000]={0};
scanf("%d",&m);
while(m--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
qsort(a,n,sizeof(a[0]),cmp); // qsort函数
for(i=0;i<n-1;i++)
printf("%d ",a[i]);
printf("%d\n",a[n-1]);
}
return 0;
}
边栏推荐
- 03. Knowledge of linear algebra
- 手持振弦VH501TC采集仪的激励方法和激励电压
- 4 种 Redis 集群方案介绍 + 优缺点对比
- Configure network parameters
- What is the difference between executing count (1), count (*) and count (column name)?
- (板子)分解质因数
- Intranet penetration - Metasploit - basic post penetration command
- Learning note 1 - common attention mechanisms (plug and play)
- ubantu专题3:设置系统语言与安装搜狗输入法
- What should we think beyond technology
猜你喜欢
51单片机:I2C通信
【MySQL从入门到精通】【高级篇】(六)MySQL表的存储引擎,InnoDB与MyISAM的对比
In March, 2022, the graphical three-level programming problem analysis of the electronic society, including the answer: Catch Game
2022.07.17(LC_6164_数位和相等数对的最大和)
The usage difference between isempty and isblank is that so many people can't answer
数据库扩容也可以如此丝滑,MySQL千亿级数据生产环境扩容实战
mysql45讲之全局锁和表锁和行锁
What are the benefits of Internet of things edge computing gateways?
51 single chip microcomputer: I2C communication
知识体系:如何构建自己的知识体系
随机推荐
03.线性代数知识
QVector、QLinkedList、QList
Notes OpenSSL - différences entre PKcs # 1 et PKcs # 8 et API appelées respectivement
【C语言】收官之战——文件的编译和连接
Analysis of graphical three-level programming problem of electronic society with answer: it snows in winter
AcWing 378. Knight placement problem solution (maximum independent set =n- minimum point coverage =n- maximum edge matching)
选择排序/插入排序/冒泡排序
mysql45讲之深入浅出索引
关于复制粘贴快捷键失效问题的解决方法
事件、事件类型、事件对象、元素盒子模型【DOM(三)】
Optaplanner learning notes (x) constraint configuration: dynamically adjust constraint weights
QT signal and slot
In the summer dehumidification battle, Hisense central air conditioning has the ultimate coup
【免费好用】节假日查询接口
51单片机:I2C通信
mysql45讲之基础架构:一条SQL查询语句是如何执行的?
leetcode:934. 最短的桥
2022.07.17(LC_6164_数位和相等数对的最大和)
一文了解 NebulaGraph 上的 Spark 项目
Ubantu topic 3: setting system language and installing Sogou input method