CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

求助大家一个简单的数组排序算法!!谢谢!

楼主lingyun2003(虚心求教)2004-12-01 22:47:35 在 C/C++ / C++ 语言 提问

要求对数组里面的元素,统计出现的次数,然后按自小到大的顺序输出;  
  比如:3     -1     3     2     2  
  输出:  
    数字       次数  
      -1           1  
        2             2  
      3               2  
  谢谢大家指点,我能统计次数,但是不能顺序输出,并且输出数组的元素还是重复的,不合要求! 问题点数:20、回复次数:7Top

1 楼zhangfjj(小张)回复于 2004-12-01 22:58:50 得分 0

冒泡呀、选择呀,多的是Top

2 楼notdefined(未定义)回复于 2004-12-01 23:09:47 得分 0

桶排序,顺手就统计了出现次数了Top

3 楼daidodo(火箭发射机)回复于 2004-12-02 12:40:04 得分 0

如果元素值范围比较小,考虑用数组统计,快而且容易。Top

4 楼kv4000(ABCD(A Brave Csharp Developer))回复于 2004-12-06 09:46:20 得分 15

#include<iostream>  
  #include<ctime>  
  using   namespace   std;  
  void   fill_array(int   a[],int   max,int   &length);  
  void   statistic(int   a[],int   length   );  
  void   swap(int   &x,int   &y);  
  int   main()  
  {  
  const   int   max=5;  
   
  double   start,end;  
  int   arr[max],length;  
  start=static_cast<double>(clock());  
  fill_array(arr,max,length);  
  statistic(arr,length   );  
  end=static_cast<double>(clock());  
  cout<<endl;  
  cout<<"本程序所用时间是:"<<(end-start)/CLK_TCK<<endl;  
  return   0;  
   
  }  
  void   fill_array(int   a[],int   max,int   &length)  
  {  
        int   next,index=0;  
  cout<<"请输入"<<max<<"个数字,以-1结束!\n";  
  cin>>next;  
  while((next!=-1)&&(index<max))  
  {  
  a[index]=next;  
  index++;  
  cin>>next;  
  }  
  length=index;  
  }  
  void   statistic(int   a[],int   length   )  
  {  
      int   temp,index=1;  
      bool   noappear=true;  
      cout<<"数组中的内容   :               出现的次数"<<endl;  
      for(int   i=0;i<length-1;i++)  
      {  
      for(int   j=i+1;j<length;j++)  
            if   ((a[i]<a[j]))  
                  swap(a[i],a[j]);  
      }  
      for(int   i=0;i<length;i++)  
      {  
            if(a[i]==a[i+1])  
                  index++;  
    else  
    if   (index==1)  
    {  
    cout<<a[i]<<"     ";  
    cout<<"1次"<<endl;  
    }  
    else  
    {  
    cout<<a[i]<<"     ";  
    cout<<index<<"次"<<endl;  
                    index=1;  
    }      
      }  
   
  }  
  void   swap(int   &x,int   &y)  
  {  
  int   temp;  
  temp=x;  
  x=y;  
  y=temp;  
  }Top

5 楼pcboyxhy(-273.15℃)回复于 2004-12-06 10:11:01 得分 5

#include<algorithm>  
  #include<iostream>  
  #include<vector>  
  #include<stdlib.h>  
  using   namespace   std;  
  int   main()  
  {  
          vector<int>v;  
          int   input;  
          for(   ;   cin>>input;   )  
          {  
                  if(input==-999)  
                          break;  
                  v.push_back(input);  
          }          
          sort(v.begin(),v.end());  
          for(int   i=0;i<v.size();i++)  
                  cout<<i<<":"<<v[i]<<endl;  
          system("PAUSE");  
  }  
  Top

6 楼pcboyxhy(-273.15℃)回复于 2004-12-06 10:12:11 得分 0

统计数据可以用hash表Top

7 楼xuzheng318(忧郁王子)回复于 2004-12-06 10:19:53 得分 0

HASH表确实能解决你的问题,下面的Sample是SGI版的STL中的例子:  
   
  void   lookup(const   hash_set<const   char*,   hash<const   char*>,   eqstr>&   Set,  
                          const   char*   word)  
  {  
      hash_set<const   char*,   hash<const   char*>,   eqstr>::const_iterator   it  
          =   Set.find(word);  
      cout   <<   word   <<   ":   "  
                <<   (it   !=   Set.end()   ?   "present"   :   "not   present")  
                <<   endl;  
  }  
   
  但是hash_set这个类属于STL的扩展,   其它版本的STL(如VC中带的)不一定有此类.   建议你用SGI的STL.  
  Top

相关问题

  • 寻找int[k]数组的快速排序算法
  • 求给数组排序的算法或其它方法
  • 数组算法??
  • 数组排序
  • 数组排序?
  • 数组排序?
  • ◆数组排序
  • 数组对掉,请看看,一个简单算法
  • 数组排序----急
  • 求一个简单的数组排序方法

关键词

  • 数组
  • 输出
  • 次数
  • 元素
  • arr
  • statistic
  • max
  • length
  • cout
  • input

得分解答快速导航

  • 帖主:lingyun2003
  • kv4000
  • pcboyxhy

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo