dedecms学校网站模板,西安网站搭建费用,珠海网站建设 骏域网站,苏州网页优化软件前言
先做赌徒的#xff0c;结果发现这道题太TM简单了#xff0c;然后就先做这道题了。 正题
给出n#xff08;1n200000#xff09;个数字#xff0c;每个数字不超过1500000000。求每个数字出现的次数 输入输出#xff08;建议无视#xff09;
Input 输入包含…前言
先做赌徒的结果发现这道题太TM简单了然后就先做这道题了。 正题
给出n1n200000个数字每个数字不超过1500000000。求每个数字出现的次数 输入输出建议无视
Input 输入包含n1行 第一行是整数n表示自然数的个数 第2~n1每行一个自然数。
Output 输出包含m行m为n个自然数中不相同数的个数按照自然数从小到大的顺序输出。每行输出两个整数分别是自然数和该数出现的次数其间用一个空格隔开。
Sample Input
8 2 4 2 4 5 100 2 100
Sample Output
2 3 4 2 5 1 100 2 解题思路
这只是哈希的练习题不代表哈希是最优解反正也能过对吧(⊙v⊙)。用哈希统计 代码
#includecstdio
#includealgorithm
using namespace std;
const int maxn299993;
struct hashs{int c,num;
};
hashs hash[maxn];
int n,x;
int hashmath(int x)//哈希函数
{return x%maxn;
}
int locate(int x)//寻找位置
{int i0,whashmath(x);while (imaxn hash[(wi)%maxn].c!0 hash[(wi)%maxn].c!x)i;return (wi)%maxn;
}
void ins(int x)//插入元素
{int wlocate(x);hash[w].cx;hash[w].num;//统计次数
}
bool find(int x)//查找
{int wlocate(x);if (hash[w].cx) return true;else return false;
}
bool cmp(hashs x,hashs y)//排序
{if (x.num0 || y.num0) return x.numy.num;return x.cy.c;
}
int main()
{scanf(%d,n);for (int i1;in;i){scanf(%d,x);ins(x);//加入}sort(hash,hashmaxn-1,cmp);//排序int i0;while (hash[i].num!0){printf(%d %d\n,hash[i].c,hash[i].num);i;//输出}
}