找出一堆数里面出现次数最多或最多的前N个数字
来源:互联网 发布:h3c dhcp mac地址绑定 编辑:程序博客网 时间:2024/05/22 01:46
描叙:一大推数据里面,数字与数字之间用空格隔开,找出出现次数最多的前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类型)。
- 找出一堆数里面出现次数最多或最多的前N个数字
- 给定n个正整数,找出它们中出现次数最多的数。
- 找出出现次数最多的数
- 提取一堆数字中出现次数最多的数字
- java 找出n个元素数组中重复次数最多的数(假设出现次数大于n/2)
- 找出数组里面元素出现次数最多的问题
- 找出数组里面元素出现次数最多的问题
- 海量数据中找出出现次数最多的前10个URL
- 找出数组中出现次数最多的数字
- 找出出现次数最多的数字(需要考虑顺序)
- 找出n个字符串中出现次数最多的子串
- 找出n个字符串中出现次数最多的字符串(创新工场)
- 找出数组中出现次数最多的数
- 在1000个数中找出出现次数最多的数
- 找出数组中出现次数最多的数
- 统计一个整数里面出现次数最多最大的数字
- 找出字符串中第一个出现次数最多的字符
- 找出字符串中第一个出现次数最多的字符
- 设置QTP replaytype
- C语言学习_002_C语言概述
- Linux的启动流程(/etc/inittab)
- RedHat linux AS 5下VirtualBox3.1.6增强功能的安装
- mysql dump备份和恢复
- 找出一堆数里面出现次数最多或最多的前N个数字
- socket(AF_INET, SOCK_RAW, IPPROTO_IP)的意思
- Mobile Web App发展现状及展望
- zeroMQ/jzmq 安装
- hibernate配置属性hbm2dll.auto初步理解
- 对C++ STL,list元素的循环删除操作 //断链,出错地方
- flex菜鸟必知一
- 使用CALL和EXEC調用存儲過程的區別
- Python正则表达式实例