zoukankan      html  css  js  c++  java
  • 2020

    绝对值从大到小排序

    冒泡排序

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    	int n;
    	
    	while (scanf_s("%d", &n) && n)
    	{
    		int i, j, t, s[101] = { 0 };
    		for (i = 0; i < n; i++)
    			scanf_s("%d", &s[i]);
    		for (i = 0; i < n; i++)
    		{
    			//冒泡排序
    			for (j = 0; j < n - 1 - i; j++)
    			{
    				if (abs(s[j]) < abs(s[j + 1]))
    					t = s[j + 1]; s[j + 1] = s[j]; s[j] = t;
    			}
    		}
    		for (i = 0; i < n; i++)
    			printf("%d%c",s[i],(i < n -1 ? ' ':'
    '));
    
    	}
    	return 0;
    }
    

      

    参考答案

    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int cmp(const int *a, const int *b)
    {
        return abs(*b) - abs(*a);
    }
    
    int main(void)
    {
        int n, i, x[101];
        
        while (scanf("%d", &n), n)
        {
            for (i = 0 ; i < n ; i++)
                scanf("%d", x + i);
            qsort(x, n, sizeof(int), cmp);
            for (i = 0 ; i < n ; i++)
                printf("%d%c", x[i], (i != n - 1 ? ' ' : '
    '));
        }
    
        return 0;
    }
    

      

    ========================if i have some wrong, please give me a message, thx.========================
  • 相关阅读:
    HRBUST 1849 商品中心
    UVA 11600 Masud Rana
    Codeforces Round #580 (Div.1)
    loj 6270 数据结构板子题
    luogu P1758 [NOI2009]管道取珠
    luogu P1852 [国家集训队]跳跳棋
    51nod 2589 快速讨伐
    SICP_3.9-3.11
    SICP_3.7-3.8
    SICP_3.5-3.6
  • 原文地址:https://www.cnblogs.com/ailx10/p/5336949.html
Copyright © 2011-2022 走看看