当前位置:网站首页>TZC 1283: simple sorting - function method

TZC 1283: simple sorting - function method

2022-07-26 05:17:00 Orange teacher

We use TZC 1283 As an example, briefly explain the sorting ( Including ten classic sorting algorithms ) Of python Implementation method and C Implementation method . First, the function method ,python Can be used in sort function ,C Language can be used qsort Function implementation .

Original link :1283: Simple order


Python It's easier to write , The code is as follows :

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 Language is not like C++ or python, No, sort function , Only qsort function , It's not so convenient to use , You need to customize the comparison function 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 function  
		for(i=0;i<n-1;i++)
		   printf("%d ",a[i]);
		printf("%d\n",a[n-1]);
	}
	return 0;
}

原网站

版权声明
本文为[Orange teacher]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/201/202207182023260077.html