• 初学快排解决绝对值排序


    输入n(n<=100)个整数,按照绝对值从大到小排序后输出。题目保证对于每一个测试实例,所有的数的绝对值都不相等。
     
    Input
    输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整数,n=0表示输入数据的结束,不做处理。
     
    Output
    对于每个测试实例,输出排序后的结果,两个数之间用一个空格隔开。每个测试实例占一行。
     
    Sample Input
    3 3 -4 2 4 0 1 2 -3 0
     
    Sample Output
    -4 3 2 -3 2 1 0
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define    M 101//; 宏定义后不加;
    
    int cmp(const void *a, const void *b)
    {
    
            //return abs(*(int *a)) < abs(*(int *b));
        //    return abs(*(int *)a) < abs(*(int *)b);//(int *)是将原来的void *型强制转换
            return abs(*(int *)b) - abs(*(int *)a);//写成这样就AC了! 
    }
    
    int main()
    {
    
            int n, i, s[M];
            while(scanf("%d", &n) && n != 0){
    
                
                for(i = 0; i < n; i++){
    
                        scanf("%d", &s[i]);
    
                }
                if(n == 1)
                {
                    
                        printf("%d
    ", s[0]);
                        continue;
                        
                }
                qsort(s, n, sizeof(s[0]), cmp);
                for(i = 0; i < n; i++){
    
                        if(i < n - 1)
                            printf("%d ", s[i]);
                        else
                            printf("%d", s[i]);
    
                }
                putchar('
    ');
    
            }
            return 0;
            
    }
    View Code
  • 相关阅读:
    虚树
    最小树形图
    分块
    斜率优化
    单调队列优化DP
    树套树
    2-SAT
    莫队
    单调队列
    单调栈
  • 原文地址:https://www.cnblogs.com/the-one/p/3238504.html
Copyright © 2020-2023  润新知