找出一堆数里面出现次数最多或最多的前N个数字

描叙:一大推数据里面,数字与数字之间用空格隔开,找出出现次数最多的前N个。

#当数字之间的空格只有一个的时候

#sed 's/ /\n/g' data.txt | sort | uniq -c | sort  -k1n -k2n | tail -10 > result.txt  

#存在的问题,当出现次数为第10的时候,与该次数可能有多个?

#解决方案:按照降序排列,找出第10行,并且取出第二个字段data,再从该文件的10行到最后一行,找出第二个字段为data的字段,最后打印出该文件中的前9行;至此问题便解决了。sed -n 10p tianmo.txt | awk '{print $1}'  > temp.txt    ;  sed -n 1,+9p filename.txt

#tr -s " "  "\n"  <  data.txt | sort | uniq -c | sort  -k1n -k2n | tail -10 > result.txt 

#当数字之间的空格可能有多个的时候

#sed 's/  */ /g' data.txt | sed 's/ /\n/g' | sort | uniq -c | sort -k1n -k2n | tail -10 > result.txt

#tr -s  " "    <    data.txt | sed 's/ /\n/g' | sort | uniq -c | sort -k1n -k2n | tail -10 > result.txt
#当数字中可能出现字符的时候

#tr -d [a-zA-Z] < data.txt | tr -s  " " | tr -s " " "\n" | sort | uniq -c | sort -k1n -k2n | tail -10 > result.txt

#tr -d [a-zA-Z] < data.txt | tr -s  " " | tr -s " " "\n" | sort | uniq -c | sort -k1nr -k2nr | head -10 > result.txt

#tr -d [a-zA-Z] < data.txt | tr -s  " " | sed 's/ /\n/g' | sort | uniq -c | sort -k1n -k2n | tail -10 > result.txt

#tr -d [a-zA-Z] < data.txt | tr -s  "  *" " " | sed 's/ /\n/g' | sort | uniq -c | sort -k1n -k2n | tail -10 > result.txt

找出出现次数最多的一个数字的算法

#include <stdio.h>


void FindMostTimesDigit(int *Src, int SrcLen)
{


	int element, has = SrcLen;

	int MaxNum, TempCount = 0, MaxCount = 0;

	int  i,j,*result = new int[ ]; 

	while(0 != has)
	{
		element = Src[has - 1];

		TempCount = 0;
		
		for (j = has - 1; j >= 0; -- j)   
		{      
			// 如果找到,则计数加1,然后将数据和末尾交换      
			// 这也是为何要从末尾开始循环的理由      
			if(element == Src[j])  
			{      
			  TempCount ++; 			  
			  // 把后面的数据移动到前面来         
			  Src[ j ] = Src[has - 1];      
          
			  has--;
			} 
		} 
		
		if(TempCount > MaxCount)  
		{      
			MaxCount = TempCount;   
			MaxNum = 0;


			result[ MaxNum ] = element; 
		} 
		else if(TempCount == MaxCount)  
		{
			
			result[ ++ MaxNum ] = element; 
		}
	}
	printf("出现最多的次数:%d\n",MaxCount);

	for( i = 0; i <= MaxNum; ++ i)
	{
		printf("%d ",result[i]);
	}
	printf("\n");
}
int main()
{
	int list[]={1,2,3,4,3,3,2,2,1,1,4,4,4,1,2};

      int length =sizeof(list) / sizeof(int); 
	
	FindMostTimesDigit(list, length);

        return 0;  
}

找出出现次数最多的前N个的算法

//示例代码:输入单词,统计单词出现次数并按照单词出现次数从多到少排序  
//也可以利用统计字符串,数字的个数等
#include <map>  
#include <vector>  
#include <string>  
#include <algorithm>  
#include <iostream> 

using namespace std; 
   
int cmpByValue(const pair<string, int>& x, const pair<string, int>& y)  
{ 
	return x.second > y.second;  
}  
   
void sortMapByValue(map<string, int>& tMap, vector<pair<string, int> >& tVector)  
{ 	
	map<string, int>::iterator curr;
	for (curr = tMap.begin(); curr != tMap.end(); curr++)  
	{  
		tVector.push_back(make_pair(curr->first, curr->second));  
	}  
  
	sort(tVector.begin(), tVector.end(), cmpByValue);  
}  

void FindAppearTimesTop(FILE *fp)
{
	int temp;
	string word; 
	char str[32];
	map<string, int> tMap; 	
	map<string, int>::iterator iter;
	
	while (NULL == feof(fp)) 
	{ 		 
		fscanf(fp,"%d",&temp); 
		 
		itoa(temp, str, 10); 
		 
		word = (string)str;
		 
		//pair< map<string, int>::iterator, bool> ret = tMap.insert(make_pair(word, 1));
		//if (!ret.second)  
		//{
		// ++ ret.first->second;  
		//}		
		iter =  tMap.find(word);
		if(iter != tMap.end())
		{			
			tMap[ iter->first ] ++;
		}
		else
		{
			tMap.insert(make_pair(word, 1));
		}	

	 }   
   
	 vector<pair<string,int> > tVector; 
 
	 sortMapByValue(tMap,tVector);  

	 for(int i = 0; i < tVector.size(); ++ i)  
	 {  
		cout<<tVector[i].first<<": "<<tVector[i].second<<endl;  
	 } 
}
   
int main()  
{  
	FILE *fp;	
	
	if(NULL == (fp = fopen("data.txt","r")))  
    {  
        printf("File read error!\n");  
  
        exit(1);  
    }   

	FindAppearTimesTop(fp);

	fclose(fp);
	
	return 0;  
}

待续 。。。

pair,make_pair学习

pair  vs  make_pair
make_pair constructs a pair object.
template < class  T1, class  T2>
pair<T1, T2> make_pair(T1 x, T2 y)
{
     return  pair<T1, T2>(x, y);
}
 
eg:  std::pair<std::string, double >( "sn001" , 12.5);
        std::make_pair( "sn001" , 12.5);
        两者效果一样。
倘若:std::pair<std::string, float >( "sn002" , 12.6);   // 12.6's datatype is float
         std::make_pair( "sn002" ,12.6);  // 12.6's datatype is double
使用:
         std::pair<std::string, double > m_pairA;
         m_pairA = std::make_pair( "sn001" , 12.5);
         std::cout<<m_pairA.first<< "  " <<m_pairA.second<<std::endl;
结合map的简单使用:
         std::pair<std::string, double > m_pairA;
         m_pairA = std::make_pair( "sn001" , 12.5);
         //std::cout<<m_pairA.first<<"  "<<m_pairA.second<<std::endl;
         std::map<std::string, double > m_mapA;
         m_mapA.insert(m_pairA);
         std::map<std::string, double >::iterator iter = m_mapA.begin();
         std::cout<<iter->first<< "  " <<iter->second<<std::endl;
小结:
         make_pair创建的是一个pair对象。使用都很方便,针对成对出现的数据,如书的ISBN对应一个书名。
         pair是单个数据对的操作,pair是一 struct 类型,有两个成员变量,通过first,second来访问,用的是“.”访问。
         map是一个关联容器,里面存放的是键值对,容器中每一元素都是pair类型,通过map的insert()方法来插入元素(pair类型)。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值