爆刷PAT(甲级)——之【1145】 Hashing - Average Search Time(25 分)——Hash

PAT甲级1145 Hashing - Average Search Time (25分) 二次探测法知识点讲解 这道题需要额外补充的知识点是Quadratic probing和Average Search Time。题目都没有解释,需要知识储备,所以当年不知道这道题是难倒了多少人。 Quadratic probing (二次探测法) 简而言之,探测间隔为平方数。 处理冲突的函数为 H(key)=(key+i*i)%TSize {i=1,2,3,...} 本题的哈希函数为H(key)=key%TS... 阅读详情

艰难的英语单词:

Quadratic probing    平方探测

increments    英 ['ɪnkrəmənts]        n.增量;

题意:给一串数,把它们按照 num%v 哈希,如果有冲突就按照平方探测法正向解决冲突,哈希不进去就输出失败。

然后给一部分刚刚的数,判断它们是否在哈希入了表内。并最后输出总共进行了多少次查询。

难点:其中好几个概念都不是很清楚——平方探测是啥、哈希的查询次数咋算什么的。

后来写代码的时候还有一个地方不理解,为什么对于每一个数值,判断它们是不是哈希成功的时候,j 的变化是从0到MAX(包括MAX),这是什么原因?因为很明显,大多数时候,用平方探测法解决冲突时候,哈希的新位置是会出现循环的,也就是说没必要使j从0到MAX才对。

上述疑问还不是很清楚答案,先按照题目的写法,把这题目AC了。

Code:

#include<bits/stdc++.h>
using namespace std;
#define inf 10009
#define INF 0x3f3f3f3f
#define loop(x,y,z) for(x=y;x<z;x++)

int n,m,len;
int e[inf];
bool isPrime(int v)
{
    int i;
    loop(i,2,sqrt(v)+1)
        if(v%i==0)return false;
    return true;
}
void Solve()
{
    scanf("%d%d%d",&len,&n,&m);
    int i,j;
    while(!isPrime(len))len++;
    loop(i,0,n)
    {
        int t;
        scanf("%d",&t);
        loop(j,0,len)
            if(!e[(t+j*j)%len])
            {
                e[(t+j*j)%len]=t;
                break;
            }
        if(j==len)printf("%d cannot be inserted.\n",t);
    }
    int sum=0;
    loop(i,0,m)
    {
        int t;
        scanf("%d",&t);
        loop(j,0,len+1)
        {
            sum++;
            if(e[(t+j*j)%len]==t||e[(t+j*j)%len]==0)
                break;
        }
    }
    printf("%.1f\n",(double)sum/m);
}
int main()
{
    Solve();
    return 0;
}

 

PAT-ADVANCED1145——Hashing - Average Search Time 我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED 原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 题目描述: 题目翻译: 1145 哈希——平均搜索时间 这个问题的任务很简单:首先在哈希表中插入一系... 阅读详情

相关推荐

PAT-A1145 Hashing - Average Search Time 题目内容及题解

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average sea...

BourbonWhiskey 的博客 724

PTA Hashing 思路分析及代码解析

Hash表(开放地址法)的实践

thexiaosi的博客 539

【机器学习】实验2,基于回归分析的大学综合得分预测(完整代码实现+分析)

大学排名是一个非常重要同时也极富挑战性与争议性的问题,一所大学的综合实力涉及科研、师资、学生等方方面面。目前全球有上百家评估机构会评估大学的综合得分进行排序,而这些机构的打分也往往并不一致。在这些评分机构中,世界大学排名中心(Center for World University Rankings,缩写CWUR)以评估教育质量、校友就业、研究成果和引用,而非依赖于调查和大学所提交的数据著称,是非常有影响力的一个。

高丽大学人工智能硕士在读|专注大模型、RAG、OCR与AI工程实践 1986

11-散列2 Hashing (PTA)

11-散列2 Hashing (25分) The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be H(key)=key%TSize where TSize is the maximum size o

qq_43791139的博客 445

大学生电赛学习之路

可以看到全国大学生电子设计大赛的认可度是非常高的,属于A类学科竞赛。电子设计大赛的形式也是给出题目,参赛者有四天三夜的时间解决问题,题目大致可以分为:控制类、测量类、高频类以及电力电子类。该竞赛会在单数年的9月份举行。但是对于新生来说电赛确实遥不可及的,可能在很多学校本科生是不能进入实验室的,但是就算进了实验室你就有资格去参加电赛或者说能拿一个不错的奖项吗? 现在正值新生开学季,当然会有一大批学生会进入实验室的学习,电赛的举办时间是每年的暑假,所以说对于大一同学来说有一年的时间去准备,那我们就来说一说参加.

weixin_46098612的博客 8796

Julia Ann老司机管理系统

https://content.badgirlsusa.com/Julia_Ann-Sexy_Julia_Ann_Fucks_A_Lil_Elf's_Big_Cock_/Julia-Ann-Naughty-America-%E6%80%A7%E6%84%9F%E7%9A%84%E8%A3%98%E8%8E%89%E5%A9%AD%E5%AE%89%E4%BA%82%E6%90%9E-lil-%E7%B2%BE%E9%9D%88%E7%9A%84%E5%A4%A7%E5%85%AC%E9%9B%9E-2020

www_oracle_com的博客 22万+

PTA 11-散列4 Hashing - Hard Version 解法详述

PTA-mooc完整题目解析及AC代码库:PTA(拼题A)-浙江大学中国大学mooc数据结构全AC代码与题目解析(C语言) 虽然课程后面专门给出了一节用来讲解这道题,但是我在做这道题的时候觉得老师的思路非常巧妙,具体实现上也有很多优化的方法,所有在这里给出了具体的解法分析详细说明 Given a hash table of size N, we can define a hash functio...

zhuiyisinian的博客 1432

如何在 SAP Server Side JavaScript 里消费 destination

在SAP云平台里打开SAP HANA Web-Based Development Workbench进行服务器端JavaScript的开发。 创建一个新的package: 创建一个新的application: 创建一个新的文件google-distance-api.xshttpdest用于维护destination相关信息: host = "maps.googlea...

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 622

PTA Hashing - Hard Version

PTA Hashing-Hard

weixin_50321513的博客 248

【嘉立创EDA】PCB 多层板如何交换内层,4层板交换板层

本文使用嘉立创EDA,描述如何在PCB设计时,如何交换各个板层,特别是4层板需要交换内层时,应该如何操作,以供有需要的读者参考。

l64481000的博客 2033

华为OD机试 - 小火车最多人时所在园区站点(Java 2024 E卷 100分)

刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、发现新题目,随时更新,全天CSDN在线答疑。

学Java,找哪吒 997

【PTA】hashing(hard version)

题意:给出一个散列表,哈希函数是H(x)=x%N,冲突处理方法是线性探测,按照散列表打印出输入的顺序,如果有多种选择先输出小的那个。 解题思路(看了姥姥的慕课和别人的代码):有冲突的元素有先后顺序,可用有向图(邻接矩阵)来存储,然后拓扑排序。 代码如下 #include<stdio.h> #include<stdlib.h> #include<queue&gt...

weixin_43719720的博客 368

Linux | Ubuntu18.04安装RTX 4060显卡驱动完整教程

本节详细介绍了如何在ubuntu18系统安装4060显卡的驱动。

九七的博客 5171

PAT 甲级 1145  Hashing - Average Search Time

1145Hashing - Average Search Time(25 point(s)) The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of inte...

漫浸天空的雨色的备忘录~ 868

YOLOv11与Roboflow数据预处理实战:从原始数据到高性能模型

摘要 Roboflow平台与YOLOv11的结合为计算机视觉开发者提供了高效的工作流。Roboflow提供数据集管理、预处理、增强和导出的一站式解决方案,支持多种标注格式并与YOLOv11无缝集成。YOLOv11在YOLO系列基础上进行了架构创新,包括深度宽度优化、C3k2机制和C2PSA注意力机制,提供从纳米版到超大版的多种模型变体。 使用Roboflow的流程包括:注册工作区、收集/导入数据(支持本地和公开数据集)、专业标注(含SAM辅助)、预处理(尺寸调整、归一化等)和数据增强(几何变换和颜色调整)。

本博客聚焦 YOLOv11 全流程落地,涵盖架构优化、数据集处理、训练技巧与多场景部署,兼及国产数据库、Java 开发与 AI 模型应用,内容兼顾理论与工程实践,为开发者提供系统干货,助力高效提升技术能力。 360

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

【PAT甲级 - C++题解】1145 Hashing - Average Search Time

📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!

Newin2020的博客 623

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

PAT甲级1145 Hashing - Average Search Time

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 欢迎使用Ma...

qq_39564678的博客 202

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

【PAT甲级A1145】Hashing - Average Search Time (25分)(c++)

1145 Hashing - Average Search Time (25分) 作者:CHEN, Yue 单位:浙江大学 代码长度限制:16 KB 时间限制:400 ms 内存限制:64 MB The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys

小庄同学的博客 243

PAT甲级 1145 Hashing - Average Search Time (25 分) 详解哈希冲突

\quad这个题是考解决哈希冲突时候采取平方探测法来建立哈希表。关于哈希的知识点总结如下: 1、两种方法简历哈希表 直接定址法: H(key)=key or H(key)=a*key+b; 除留取余法: H(key)=key%mod,当mod是一个素数时H(key)能覆盖[0,mod-1]范围内每一个数,Tsize=mod 2、两种方法解决冲突 线性探测法(Linear Probing) ...

qq_40438165的博客 1550

PAT甲级1145 Hashing - Average Search Time (25 分)题解

$\quad平方探测法请移步博客。这里说一下查询操作,查询当前数在哈希表中所在位置或者是计算查询次数需要再次模拟插入过程,当找到这个数或者发现这个位置上没有数时表示已经找到这个数或者无此数,无需再查询。程序如下: #include <iostream> #include <cstring> using namespace std; const int maxn = 1e...

qq_40438165的博客 1537

PAT真题练习(甲级)1145 Hashing - Average Search Time (25 分)

PAT真题练习(甲级)1145 Hashing - Average Search Time (25 分) 原题网址:https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this problem is simple: insert a sequence of disti...

Be Quick & Clever & Lucky 325

PAT 1145 Hashing - Average Search Time

原题连接:1145 Hashing - Average Search Time (25分) 参考的柳神博客:1145. Hashing – Average Search Time (25) – 甲级 题目:The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer

cwtnice的博客 209

pat甲级 hash A1145 Hashing - Average Search Time (25分)

1145 Hashing - Average Search Time (25分) The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the

weixin_42358356的博客 137

PAT——1145 Hashing - Average Search Time 甲级

1145 Hashing - Average Search Time题目题意AC代码参考注意 题目 https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 题意 创建哈希序列,用平方探测法解决哈希冲突,如果不能插入则输出”X cannot be inserted.”,最后输出平均查找时间 AC代码 #include<bits/stdc++.h> using namespace std; int i

ljhsq的博客 260

甲级PAT 1145 Hashing - Average Search Time (25 分)(hash,二次方探测)

1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer key...

Joyceyang_999的博客 472

PAT_甲级_1145 Hashing - Average Search Time (25point(s)) (C++)【哈希表/平方正向探测法】

1,题目描述 Sample Input: 4 5 4 10 6 4 15 11 11 4 15 2 Sample Output: 15 cannot be inserted. 2.8 2,思路 3,AC代码 4,解题过程 第一搏 #include<bits/stdc++.h> using namespace std; int ...

&再见萤火虫&的博客 409
上一篇: 爆刷PAT(甲级)——之【1144】 The Missing Number(20 分)——排序
下一篇: 爆刷PAT(甲级)——之【1142】 Maximal Clique(25 分)——图论
仰天倀笑
博客等级 码龄10年 313粉丝 124原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值