~追逐的风 2019-08-16 14:52 采纳率: 0%
浏览 260

求助:关于散列的一道题,为什么最后只输出 0

自学数据结构,遇到一道散列的题,程序有错,一下午都没有解决,求大神帮忙。
PTA 上一道题:
原题如下:
输入首先给出正整数N(≤10^5)为通话记录条数。随后N行,每行给出一条通话记录。列出拨出方和接收方的11位数字构成的手机号码,其中以空格分隔。
输出: 在一行中给出聊天狂人的手机号码及其通话次数,其间以空格分隔。如果这样的人不唯一,则输出狂人中最小的号码及其通话次数,并且附加给出并列狂人的人数 。
输入样例:
4
13005711862 13588625832
13505711862 13088625832
13588625832 18087925832
15005713862 13588625832
输出样例:
13588625832 3
我的代码
#include
#include
using namespace std;
typedef struct node
{
string data;
int count;
struct node* next;
}* List;
typedef struct HTbnode
{
int tablesize;
List Heads;
}* HashTable;
int nextprim(int n)
{
int m=(n%2)?n+2:n+1,i;
while(1)
{
for(i=3;i*i<=m;i+=2)
if(m%i==0) break;
if(i*i>m) break;
else m+=2;
}
return m;
}
HashTable CreateTable(int n)
{
HashTable T=new HTbnode;
T->tablesize=nextprim(n);
T->Heads=new node[T->tablesize];
for(int i=0;itablesize;++i)
{
T->Heads[i].next=NULL;
T->Heads[i].count=0;
}
return T;
}
int Hash(string data,int tablesize)
{
string::size_type m=data.size()-5;
int i=0,hash=0;
while(i++ hash=hash*10+(data[m++]-'0');
return hash%tablesize;
}
List Find(HashTable H,string data)
{
int pos=Hash(data,H->tablesize);
List p=H->Heads[pos].next;
while(p&&p->data!=data)
p=p->next;
return p;
}
bool insert(HashTable H,string data)
{
List p=Find(H,data);
if(p)
{
(p->count)++;
return false;
}
else
{
int pos=Hash(data,H->tablesize);
struct node q=H->Heads[pos];
List DataNode=new node;
DataNode->count=1;
DataNode->data=data;
DataNode->next=q.next;
q.next=DataNode;
return true;
}
}
void print(HashTable H)
{
List p;
string maxstr;
int max=0,num=0;
for(int i=0;itablesize;++i)
{
p=H->Heads[i].next;
while(p)
{
if(p->count>max)
{
max=p->count;
num=1;
maxstr=p->data;
}
else if(p->count==max)
{
num++;
if(maxstr>p->data)
maxstr=p->data;
}
p=p->next;
}
}
cout< if(num>1) cout<<' '< }
int main()
{
int n,i=0;
cin>>n;
string str;
HashTable H=CreateTable(2*n);
while(i++ {
cin>>str;
insert(H,str);
}
print(H);
return 0;
}
```为什么输出为 0??

  • 写回答

2条回答

  • 豆丷 2019-08-16 15:40
    关注

    1.代码放代码片里
    2.写点注释,不然看你代码的时间都够重新写一次了
    3.我在PTA这道题已经做出来了 用的是c++的vector容器

    评论

报告相同问题?

悬赏问题

  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。