一些做过的题
来源:互联网 发布:js游戏制作教程 编辑:程序博客网 时间:2024/04/29 13:02
DZY has a hash table with p buckets, numbered from0 to p - 1. He wants to insertn numbers, in the order they are given, into the hash table. For thei-th number xi, DZY will put it into the bucket numberedh(xi), whereh(x) is the hash function. In this problem we will assume, thath(x) = x mod p. Operationa mod b denotes taking a remainder after divisiona by b.
However, each bucket can contain no more than one element. If DZY wants to insert an number into a bucket which is already filled, we say a "conflict" happens. Suppose the first conflict happens right after thei-th insertion, you should output i. If no conflict happens, just output -1.
Input
The first line contains two integers, p andn(2 ≤ p, n ≤ 300). Thenn lines follow. The i-th of them contains an integer xi(0 ≤ xi ≤ 109).
Output
Output a single integer — the answer to the problem.
Sample Input
Input
10 5021534153
Output
4
Input
5 501234
Output
-1
题目大意:找到第一个余数重复出现的编号;将一个数组全初始化为0,余数作为数组下标,如a[1],a[2]...,重复出现的就给count加,此处count就是记录的第一个重复出现的。
#include<stdio.h>int a[303];int main(){int p,n,i,count=-1,flag=1;scanf("%d%d",&p,&n);for(i=0;i<n;i++){int x;scanf("%d",&x);if(flag){if(a[x%p]==0)a[x%p]=1;else{count=i+1;flag=0;}}}printf("%d",count);return 0;}
- 一些做过的题
- 做过的一些水题的总结
- 今天来整理一下做过的一些题吧
- 我做过的一些共享软件
- 总结一些做过的习题
- 一些做的过的零散的题目(poj)
- 个人的一些介绍和做过的项目,呵呵
- 再说下自己做过的一些系统,呵呵
- 我曾经做过的一些Android开发项目
- 简要总结我做过的一些项目
- 没做过自动化测试的一些初级想法
- ASP.NET Web Form 一些做过的东西
- 做过的一些sql题目,附答案(自己做的哦)
- 留点做过的题,以后用
- 一位程序员做过的笔试题
- 做过的题目
- 做过的项目
- 对自己之前做过项目所做的一些笔记
- 第7周 C语言及程序设计提高例程-27 编写查找和排序函数(有疑问)
- Struts2 使用FileUtils上传文件--文件名一定不能有中文!!!
- python调用java函数
- DDL、DML、DCL、DQL
- vim 与 Ctrl+q
- 一些做过的题
- Zero Sum(DFS)
- 对Xpath爬取网页数据的理解
- c++实验五-数组选择
- Java设计模式之工厂方法、抽象工厂模式 程序 总结
- Android中px、dp、sp,这些单位的区别
- null==0问题
- Mina框架学习
- 适配器模式:把方块放进圆洞