zoukankan      html  css  js  c++  java
  • 每日一练之排序算法(P1097 统计数字)

    某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5×10^9)。已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统计结果。

    采用sort直接排序,直接略过排序门槛;

     1 //P1097 统计数字
     2 #include<iostream>
     3 #include<algorithm>
     4 using namespace std;
     5 //const int MAX=200001;
     6 
     7 int main()
     8 {
     9     int i,n,count;
    10     cin>>n;
    11     int *a=new int[n];
    12     for(i=0;i<n;i++)    cin>>a[i];
    13     sort(a,a+n);
    14 
    15     for(i=0;i<n;i++)
    16     {
    17         count=1;
    18         cout<<a[i]<<" ";
    19         while(a[i+1]==a[i])  //如果后一位数相同,则统计叠加,否则输出统计结果
    20         {
    21             count++;i++;
    22         }
    23         cout<<count<<endl;
    24     }
    25 
    26     delete []a;
    27     return 0;
    28 }

    注:sort(start,end+1,排序方法),start为排序起始地址,而end+1是指实际上是最后一个排序地址的下一个地址(为什么要+1呢?因为这样的设计是整个stl的设计原则决定的,STL的容器在传递迭代器参数时都是传递容器的开始位置,以及容器结尾的下一位置);排序方法默认升序,也可重写,注意返回bool型或int型。

    补上快排与堆排序的代码:

     1 void QuickSort(int a[],int left,int right)
     2 {
     3     if(left<right)
     4     {
     5         int i=Division(a,left,right);
     6         QuickSort(a,left,i-1);
     7         QuickSort(a,i+1,right);
     8     }
     9 }
    10 
    11 
    12 int Division(int a[],int left,int right)     //常用的找分界点方法
    13 {
    14     int temp=a[left];
    15     while(left<right)
    16     {
    17         while(left<right&&a[right]>=temp)  right--;
    18         a[left]=a[right];
    19         while(left<right&&a[left]<=temp)    left++;
    20         a[right]=a[left];
    21     }
    22     a[left]=temp;
    23     return left;
    24 }
    25 
    26 int Division(int a[],int left,int right)   //另一种方式,通过将小于等于基准值的数全部集中到基准点左侧,count值即为最终基准值所在序号
    27 {
    28     int t,temp=a[left];
    29     int count=left;
    30     for(int i=left+1;i<=right;i++)  
    31     {
    32         if(a[i]<=temp)
    33         {
    34             count++;
    35             t=a[i];
    36             a[i]=a[count];
    37             a[count]=t;
    38         }
    39     }
    40     t=a[left]; 
    41     a[left]=a[count];
    42     a[count]=t;
    43     return count;
    44 
    45 }
    46 
    47 
    48 
    49 ////////堆排
    50 void HeapSort(int a[],int n)
    51 {
    52     for(int i=n/2-1;i>=0;i--)    HeapAdjust(a,i,n);
    53     for(int i=n-1;i>0;i--)    
    54     {
    55         int t=a[0];
    56         a[0]=a[i];
    57         a[i]=t;
    58         HeapAdjust(a,0,i);
    59     }
    60 }
    61 
    62 void HeapAdjust(int a[],int s,int n)
    63 {
    64     int j,temp;
    65     while(2*s+1<n)
    66     {
    67         j=2*s+1;
    68         if((j+1<n)&&(a[j+1]>a[j]))    j++;
    69         if(a[s]<a[j])
    70         {
    71             temp=a[s];
    72             a[s]=a[j];
    73             a[j]=temp;
    74             s=j;
    75         }
    76         else break;
    77     }
    78 }

    最简短做法是使用map与set, 一个统计数,一个统计值;

     1 //#include<bits/stdc++.h>
     2 #include<iostream>
     3 #include<map>
     4 #include<set>
     5 
     6 using namespace std;
     7 int main() {
     8     map <int,int> num;
     9     set <int> a;
    10     int n,i,q;
    11     cin>>n;
    12     for(i=1; i<=n; i++) {
    13         cin>>q;
    14         num[q]++;
    15         a.insert(q);
    16     }
    17     for(set<int>::iterator it=a.begin(); it!=a.end(); it++)
    18         cout<<*it<<" "<<num[*it]<<endl;
    19 }
  • 相关阅读:
    C#中的编译为什么不叫Compile而叫build
    类型(Type)
    C#中关于值类型和引用类型的区别
    php通用化api格式输出
    tp6获取参数的五种办法
    PHP 数组------分割、合并
    explain结果中的type字段的含义
    order by 排序
    SQL开发技巧
    tp5模板输出日期时间
  • 原文地址:https://www.cnblogs.com/maxonzou/p/10629728.html
Copyright © 2011-2022 走看看