• hdu2020绝对值排序


    总是记不住sort排序特点……一道水题记一下

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=2020

    输入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<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<math.h>
    using namespace std;
    
    struct Array
    {
        int value;
        int flag=0;
    }a[105];
    
    bool cmp(Array a,Array b)//从大到小排序 
    {
        return a.value>b.value;
    }
    
    int main()
    {
        int n;
        while(cin>>n)
        {
            if(n==0)break; 
            for(int i = 0;i < n; i++ )
            {
                cin>>a[i].value;
                if(a[i].value<0)
                {
                    a[i].value=-a[i].value;
                    a[i].flag = 1;
                }
                else a[i].flag=0;
                
            }
            sort(a,a+n,cmp);
            for(int i = 0;i < n;i ++)
            {
                if(a[i].flag==1)
                    cout<<-a[i].value;
                else cout<<a[i].value;
                if(i!=n-1)
                    cout<<" ";
                if(i==n-1)
                        cout<<endl;
            }
            
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Web 服务器配置
    Web 浏览
    C++ Web 编程
    C++ 多线程
    C++ 信号处理
    C++ 预处理器
    C++ 模板
    C++ 命名空间
    C++ 动态内存
    C++ 异常处理
  • 原文地址:https://www.cnblogs.com/lyqf/p/9737802.html
Copyright © 2020-2023  润新知