PTA_2019春_电话聊天狂人

给定大量手机用户通话记录,找出其中通话次数最多的聊天狂人。

输入格式:

输入首先给出正整数N(≤10​5​​),为通话记录条数。随后N行,每行给出一条通话记录。简单起见,这里只列出拨出方和接收方的11位数字构成的手机号码,其中以空格分隔。

输出格式:

在一行中给出聊天狂人的手机号码及其通话次数,其间以空格分隔。如果这样的人不唯一,则输出狂人中最小的号码及其通话次数,并且附加给出并列狂人的人数。

输入样例:

4
13005711862 13588625832
13505711862 13088625832
13588625832 18087925832
15005713862 13588625832

输出样例:

13588625832 3

 散列的创建我是直接copyMOOC上老师给的代码,不想自己敲了,这一点要批评。

/*感觉移位法存在点问题(左移出界?),有空想一下,虽然可以跑满分*/

/*聊天狂人*/
/*一点都不想用散列查找,本来想直接暴力建个数组,虽然知道肯定会卡这个的
  一开始用数字分析法,建立的hash函数,但是总是超时,说明这个散列函数不能减少冲突
  后来用了移位法,得以成功,说明移位法还是爸爸。*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAXTABLESIZE 300000 /* 允许开辟的最大散列表长度 */
typedef char* ElementType;    /* 关键词类型用整型 */
typedef int Index;          /* 散列地址类型 */
typedef Index Position;     /* 数据所在位置与散列地址是同一类型 */
/* 散列单元状态类型,分别对应:有合法元素、空单元 */
typedef enum { Legitimate, Empty } EntryType;

typedef struct HashEntry Cell; /* 散列表单元类型 */
struct HashEntry {
	char Data[12]; /* 存放元素 */
	int num;          /*出现的次数*/
	EntryType Info;   /* 单元状态 */
};

typedef struct TblNode* HashTable; /* 散列表类型 */
struct TblNode {   /* 散列表结点定义 */
	int TableSize; /* 表的最大长度 */
	Cell* Cells;   /* 存放散列单元数据的数组 */
};
//int Hash(char* Key, int Tablesize) {    /*数字分析法,不行*/
//	int h;
//	h = (Key[4] - '0') * 1000 + (Key[6] - '0') * 100 + (Key[8] - '0') * 10 + (Key[10] - '0')+(Key[2]-'0')*10000;
	printf("%d %d \n", h, h % Tablesize);
//	return h % Tablesize;
//}
int Hash(const char* Key, int TableSize) {   /*移位法,字符串长度不大于12*/
	unsigned int h = 0; //散列函数值,初始化为0
	while (*Key != '\0') //位移映射
		h = (h << 5) + *Key++;
	return h % TableSize;
}
int NextPrime(int N)
{ /* 返回大于N且不超过MAXTABLESIZE的最小素数 */
	int i, p = (N % 2) ? N + 2 : N + 1; /*从大于N的下一个奇数开始 */

	while (p <= MAXTABLESIZE) {
		for (i = (int)sqrt(p); i > 2; i--)
			if (!(p % i)) break; /* p不是素数 */
		if (i == 2) break; /* for正常结束,说明p是素数 */
		else  p += 2; /* 否则试探下一个奇数 */
	}
	return p;
}

HashTable CreateTable(int TableSize)
{
	HashTable H;
	int i;

	H = (HashTable)malloc(sizeof(struct TblNode));
	/* 保证散列表最大长度是素数 */
	H->TableSize = NextPrime(TableSize);
//	printf("%d\n", H->TableSize);
	/* 声明单元数组 */
	H->Cells = (Cell*)malloc(H->TableSize * sizeof(Cell));
	/* 初始化单元状态为“空单元” */
	for (i = 0; i < H->TableSize; i++) {
		H->Cells[i].Info = Empty;
		H->Cells[i].num = 0;
	}
	return H;
}
Position Find(HashTable H, ElementType Key)
{
	Position CurrentPos, NewPos;
	int CNum = 0; /* 记录冲突次数 */

	NewPos = CurrentPos = Hash(Key, H->TableSize); /* 初始散列位置 */
//	printf("%d\n", NewPos);
	/* 当该位置的单元非空,并且不是要找的元素时,发生冲突 */
	while (H->Cells[NewPos].Info != Empty && strcmp(H->Cells[NewPos].Data, Key)!=0) {
        /* 统计1次冲突,并判断奇偶次 */
		if (++CNum % 2) { /* 奇数次冲突 */
			NewPos = CurrentPos + (CNum + 1) * (CNum + 1) / 4; /* 增量为+[(CNum+1)/2]^2 */
			if (NewPos >= H->TableSize)
				NewPos = NewPos % H->TableSize; /* 调整为合法地址 */
		}
		else { /* 偶数次冲突 */
			NewPos = CurrentPos - CNum * CNum / 4; /* 增量为-(CNum/2)^2 */
			while (NewPos < 0)
				NewPos += H->TableSize; /* 调整为合法地址 */
		}
	}
	return NewPos; /* 此时NewPos或者是Key的位置,或者是一个空单元的位置(表示找不到)*/
}

int Insert(HashTable H, ElementType Key)
{
	Position Pos = Find(H, Key); /* 先检查Key是否已经存在 */
	//printf("%d\n", Pos);
	if (H->Cells[Pos].Info != Legitimate) { /* 如果这个单元没有被占,说明Key可以插入在此 */
		H->Cells[Pos].Info = Legitimate;
		strcpy(H->Cells[Pos].Data, Key);
		H->Cells[Pos].num++;
		/*字符串类型的关键词需要 strcpy 函数!! */
	}
	else {
		H->Cells[Pos].num++;
	}
	return H->Cells[Pos].num;
}
int main() {
	int N;
	scanf("%d", &N);
	char a[12];
	char b[12];
	HashTable H;
	H = CreateTable(2 * N);
	for (int i = 0; i < 2 * N; i++) {
		scanf("%s", a);
		Insert(H, a);
	}/*输入结束*/
	int top = 0;
	int temp_i = 2;
	for (int i = 0; i < H->TableSize; i++) {  /*初始化*/
		if (H->Cells[i].num > 0) {
			top = H->Cells[i].num;
			temp_i = i;
			break;
		}
	}
	int person = 0;
	for (int i = temp_i + 1; i < H->TableSize; i++) {
		if (H->Cells[i].num > top) {
			top = H->Cells[i].num;
			temp_i = i;
		}
		else if (H->Cells[i].num == top) {
			if (strcmp(H->Cells[i].Data, H->Cells[temp_i].Data) < 0) {  /*最小号码*/
				top = H->Cells[i].num;
				temp_i = i;
			}
		}
	}
	for (int i = 0; i < H->TableSize; i++) {
		if (H->Cells[i].num == top) {
			person++;
		}
	}
	if (person < 2) {
		printf("%s %d", H->Cells[temp_i].Data, top);
	}
	else {
		printf("%s %d %d", H->Cells[temp_i].Data, top, person);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值