P5715 【深基3.例8】三位数排序

在这里插入图片描述

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int x[3];
    cin >> x[0] >> x[1] >> x[2];
    sort(x,x+3);
    for(int i = 0;i < 3;i++)
        cout << x[i] << " ";
    
}
  • 对于水题,我们的目标就是怎样用最短的代码解决问题
  • algorithm中的sort函数

大佬

桶排序

我就简单粗暴地叙述一下这个方法:

简而言之,就是统计每个数出现的次数,然后从1 1 1 到 100 100 100扫描这个计数的数组(题目给的范围),每个数出现了几次,就输出几遍。就不存在排序的问题了。

int main()
{
    cin>>a>>b>>c;

    bucket[a]++;
    bucket[b]++;
    bucket[c]++;  //统计出现次数 

    for(re i=1;i<=100;i++)  //从1~100扫描 
        while(bucket[i]--)  //看不懂的同学们,其实这也相当于for(re i=bucket[i];i>0;i--)
            cout<<i<<' ';  //出现几次,输出几次,别忘了空格~ 

    return 0;
}
发布了372 篇原创文章 · 获赞 48 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/dghcs18/article/details/104309902