P1816统计数字
来源:互联网 发布:万象域名 编辑:程序博客网 时间:2024/05/17 09:29
P1816统计数字
好吧,快NOIP考试了,我临时抱佛脚,随便刷题了233。
今天这是一道简单(标准)(水)的哈希
#include<stdio.h>#include<stdlib.h>typedef struct nature{ int a; int sum; struct nature* next;}N;//每一个单独元素建立一个结构N hash[13];//每个元素hash存储int S[10005];int SUM=-1;int comp(const void*a,const void*b){ return *(int*)a-*(int*)b;}int add(int t)//添加新元素{ N* p; N* p1; p=&hash[t%13]; while(1) { if(t==p->a) { p->sum++; return 0; } else { if(p->next!=0) p=p->next; else break; } } if(p->next==0) { p1=(N*)malloc(sizeof(N)); p1->a=t; p1->sum=1; p1->next=0; p->next=p1; } return 0;}int search(int a)//取出所有的非重复元素{ N* p; p=hash[a].next; while(p!=0) { SUM++; S[SUM]=p->a; p=p->next; } return 0;}int read(int t)//读出{ N* p; p=hash[t%13].next; while(p->a!=t) p=p->next; printf("%d %d \n",t,p->sum); return 0;}int main(){ int n; int i,temp; scanf("%d",&n); for(i=0;i<13;i++) { hash[i].next=0; hash[i].a=-1; hash[i].sum=-1; } for(i=1;i<=n;i++) { scanf("%d",&temp); add(temp); } for(i=0;i<13;i++) search(i); qsort(S,SUM+1,sizeof(int),comp);//及其智障的“+1”,最后一位的下标为SUM,总长度为SUM+1 for(i=0;i<=SUM;i++) read(S[i]); return 0;}
啦啦,很简单,因为有最多10000个数字,最大是1.5*10^9,不好每个比较,就用哈希分类·。再通过线性表处理矛盾空间大小为O(N),时间复杂度(N^2)(即所有元素对13求余值相同,然后全部排一起。。。)
0 0
- P1816 统计数字
- P1816统计数字
- Vijos P1816 统计数字【序列处理】
- 统计数字
- 统计数字
- 统计数字
- 统计数字
- 统计数字
- 统计数字
- 统计数字
- 统计数字
- 统计数字
- 统计数字
- 统计数字
- 洛谷P1816 忠诚
- 洛谷P1816 忠诚
- 洛谷P1816 忠诚 题解
- 洛谷p1816忠诚
- EOJ 2562
- 查看 linux 系统硬件配置
- Cocos2d-x在ios模拟器上前后台切换的方法调用
- 2016/10/30
- 51nod 1202
- P1816统计数字
- 51nod算法马拉松19总结
- vue.js 计算属性$watch
- 图文结合
- Yii2.0增删改查
- Pointers on C 重点笔记 第三章
- VC_GPIB_Demo
- LeetCode 167 Two Sum II - Input array is sorted (两点法)
- 1069. The Black Hole of Numbers