PAT A 1078 Hashing

PAT-A1078 Hashing 题目内容及题解 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%TSiz... 阅读详情

原题

1078 Hashing

分数 25

全屏浏览

切换布局

作者 CHEN, Yue

单位 浙江大学

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 of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.

Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (≤104) and N (≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print "-" instead.

Sample Input:

4 4
10 6 4 15

Sample Output:

0 1 4 -

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

解法

主要使用:

  1. 筛法找素数
  2. 哈希中的平方探测法,注意index = (key + step * step) % size,  step从0到size - 1
#include <iostream>
#include <cstring>

using namespace std;
const int N = 1e4 + 10;

int h[N]; // 输入数字i在mp上的索引为h[i]
bool mp[N]; // 在mp上的某个位置是否被占用
int max_size = 0;

bool prime[N];
int find(int m) {
    for (int i = 2; i < N; i ++) {
        if (!prime[i]) continue;
        int j = i;
        int k = 1;
        do {
            j = i * k;
            prime[j] = false;
            k ++;
        }while (j <= m && j < N);

        if (i >= m) return i;
    }
    return m;
}

void insert(int x, int temp) {
    // h[x]设置为索引,索引不对则设为-1
    int k = (temp % max_size);
    int r = k;
    int d = 0;
    while (d <= max_size - 1) { // 该索引位已经被占领
        if (!mp[r]) { // 没占领
            h[x] = r;
            mp[r] = true;
            break;
        }
        d += 1;
        r = (temp + d * d) % max_size;
    }
}

int main() {
    int m, n;
    scanf("%d%d", &m, &n);
    memset(prime, true, sizeof(prime));
    memset(mp, false, sizeof(mp));
    memset(h, -1, sizeof h);

    max_size = find(m);
    for (int i = 0; i < n; i ++) {
        int temp;
        scanf("%d", &temp);
        insert(i, temp);
    }

    for (int i = 0; i < n; i ++) {
        if (i == n - 1) {
            if (h[i] != -1) printf("%d", h[i]);
            else printf("-");
        }
        else {
            if (h[i] != -1) printf("%d ", h[i]);
            else printf("- ");
        }
    }

    return 0;
}

关于mos管的压降问题(项目遇到问题总结) 在研发过程中没有出现问题的板子,到了小批时竟出现雷达一直在触发???找了大几天,没有头绪。心中疑惑颇多,加上老板催促,自考等原因,真是压力山大。4月份自考,只有差不多一个月时间了吧,没有什么大的收获,反而在查找硬件问题的过程中,发现了一个自己忽视的一个问题,就是关于Mos管的压降问题。 备考的过程崎岖不平,工作又问题多多,孰轻孰重,如何取舍?从心理来说,不想取舍什么,只是有所得就可以,过不了就过不了呗,已经毕业十五年了,没这个本科文凭不也过来了吗?只是代表了自己在这一段时间的思考,自己兴趣吧,没什么大不了 阅读详情

相关推荐

无刷直流电机矢量控制(四):基于滑模观测器的无传感器控制

无刷直流电机矢量控制,采用转速、电流双环控制,电流环dq解耦,d轴采用id=0控制,q轴采用转速外环。采用滑模观测器(SMO),根据电压电流信息进行电机位置角度动态估算,模型可直接运行,转速和角度跟踪精度较高。 本文整理了该方法的基本原理,介绍了在MATLAB/Simulink中的建模和仿真过程,最后附上完整的模型文件。 一、基本原理 滑模观测器(下文简称SMO)是基于滑模变结构控制方法的一种状态观测器。其针对无刷直流电机转子位置估算的实现过程如下。 假设采用矢量控...

Forster-C的博客 6865

PAT A1078 Hashing

PAT A1078 Hashing Sample Input: 4 4 10 6 4 15 Sample Output: 0 1 4 - 思路 1:hash二次方探测: 规则:若冲突(设冲突位置为key),不断探测key+11、key+22…key+step*step(step:从1一直取到size-1?这里为啥…先挖个坑:以后有时间要总结下各种hash探测的规则和证明~ ~ ~ ~) ...

qq_42347617的博客 186

wps js宏获取工作簿、工作表、单元格的值、链接转图片、单元格区域数据转二维数组、保存工作簿的常用Excel操作

wps js宏入门看这里,我写了常用的js操作Excel的方法:获取工作簿对象、获取工作表对象、获取单元格的值、链接转图片、生成条形码图片、单元格区域数据转二维数组方法、保存工作簿、弹出输入框、YesNo选择框,助你从VBA转战JS。

huaermeier的博客 3万+

PAT 甲级A1078 hashing

PAT甲级——A1078 Hashing 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 ...

haocoming的博客 207

微波传感器 - 从原理到实战,解锁非接触感知新维度

本文深入解析微波传感器的工作原理,重点阐述了其基于多普勒效应的非接触感知机制,并对比了红外、超声波等技术的优劣。通过树莓派Pico与RCWL-0516模块的实战案例,展示了如何构建区域入侵检测器,并探讨了微波传感器在智能家居、工业自动化及汽车电子等领域的广阔应用前景。

weixin_29306571的博客 222

随机子空间集成方法原理与scikit-learn实践

集成学习通过组合多个基学习器的预测结果来提升模型性能,是机器学习中的核心方法之一。随机子空间方法作为集成学习的重要变体,其核心原理是通过对特征空间进行随机子采样来增加模型多样性。这种方法特别适合处理高维数据,能有效缓解维度灾难问题,常与决策树、K近邻等对特征选择敏感的基学习器配合使用。在scikit-learn中,可以通过BaggingClassifier和BaggingRegressor实现随机子空间集成,关键参数包括max_features和n_estimators。实际应用中,该方法在金融风控、医疗诊

weixin_33773084的博客 376

PAT:A1078 Hashing (25 分)

PAT:A1078 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 def...

Ecloss 407

告别CPU空转!用STC8的SPI+DMA高效驱动上百颗WS2812B灯珠

本文详细解析了如何利用STC8单片机的SPI+DMA外设高效驱动WS2812B全彩灯珠,解决传统软件延时驱动方式在长灯带应用中的性能瓶颈。通过硬件方案设计、数据格式转换和DMA传输配置,实现高达400Hz的刷新率和低于5%的CPU占用率,适用于大规模灯光项目。

weixin_30847939的博客 394

PAT甲级刷题之路——A1078 Hashing

PAT甲级A1078 题如其名,哈希表题PAT甲级A1078 Hashing(25分)原题如下题目意思自己的想法答案反馈代码结语 题如其名,哈希表题 嘿嘿,既然是哈希表,那我用mapmapmap吧,STLSTLSTL爸爸我永远爱你!! 后来发现根本不需要用map,其实一个数组就能搞定了,大概这道题是考察质数的吧~ 关键词:质数 哈希表 PAT甲级A1078 Hashing(25分) 原题如下 ...

qq_43677396的博客 497

【Godot4自学手册】第一节配置Godot运行环境

Godot是一款自由开源、由社区驱动的 2D 和 3D 游戏引擎!它是一款功能丰富的跨平台游戏引擎,可以通过统一的界面创建 2D 和 3D 游戏。它提供了一套全面的通用工具,因此用户可以专注于制作游戏,而无需重新发明轮子。游戏可以一键导出到多个平台,包括主流的桌面平台(Linux、macOS、Windows)、移动平台(Android、iOS)、基于 Web 的平台以及主机平台。

zhaoyang314的博客 5981

电子元器件-三极管终终篇:三极管工作原理详解、参数/手册详解、开关应用详解、Ib电流研究、开关应用实例

三极管工作原理与应用摘要 三极管是一种电流控制器件,通过小基极电流控制大集电极电流。其内部结构由高掺杂发射区、薄低掺杂基区和中等掺杂集电区组成,形成NPN或PNP结。三极管有三种工作状态: 截止区:发射结和集电结均反偏,无放大作用 放大区:发射结正偏,集电结反偏,Ic=βIb 饱和区:两结均正偏,Ic不再随Ib增大 作为开关使用时,需确保三极管深度饱和(Vce≈0.2V)。设计要点包括: 基极电阻计算确保Ib足够大 集电极电阻控制Ic 基极加下拉电阻提高抗干扰 NPN管作地开关,PNP管作电源开关 典型应用

qq_60226558的博客 310

[PAT-A 1078]Hashing

题目大意: 给出散列表长TSize和欲插入的元素,将这些元素按读入的顺序插入散列表中,其中散列函数为H(key)=key%TSize,Hash冲突的解决采用平方探查法(正向增加)。 如果题目给叔的TSize不是素数,那么需要将TSize重新赋值为第一个比TSize大的素数元素进行插入。 思路: 1)首先对于一个输入的TSize,如果不是素数,则必须找见一个比它大的素数。 2)开一个bool类型数...

RegulusF的博客 218

推荐系统中召回率Recall计算方式附代码

Recall(召回率)是指在所有实际有交互的用户 - 物品对中,推荐系统成功预测出的比例。具体来说,设所有有交互的用户 - 物品对为S,推荐系统预测出的用户 - 物品对为T,则Recall的计算公式为:

优快云 精品推荐 1841

[PAT A1078]Hashing

[PAT A1078]Hashing 题目描述 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 defi...

欢迎来到我的编程小屋 781

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

PAT A1078 Hashing ——浊酒一杯家万里

PAT A1078 Hashing 1号测试点错误是因为判断质数时没考虑1 原来平方探测加上平方之后还要模size,我特么之前记得是错的。。。 平方探测的范围是probe < size #include <iostream> #include <algorithm> #include <string> #include <vector> #include <queue> #include <math.h> #include &

weixin_40852534的博客 182

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

【PAT】A1078 Hashing【hash】

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%TSiz...

elegant coder 667

廖雪峰Python教程[完整版]

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

PAT.A1078 Hashing

返回目录 题意 给出散列表长TSize和欲插入的元素,将这些元素按读入的顺序插入散列表中,其中散列函数为H(key) = key % TSize,解决冲突采用只往正向增加的二次探查法(即二次方探查法)。另外,如果题目给出的TSize不是素数,那么需要将TSize重新赋值为第一个比TSize大的素数再进行元素插入。 样例(可复制) 4 4 10 6 4 15 //output 0 1 4 - 注意...

大模型天花板 445

PAT A 1078 Hashing

#include<iostream> #include<math.h> using namespace std; const int maxn=100010; bool hash_table[maxn]={0}; bool IsPrime(int n) { if(n<=1) return false; int sqr=(int)sqr...

MIKET_T的博客 250

PAT | A1078 Hashing

第一次没有做碰撞检测,导致测点1和3答案错误 AC代码: #include <iostream> #include <algorithm> using namespace std; bool primes[10010]; bool hashTable[10010]; int main(){ fill(primes,primes + 10010,true); pri...

yehonglin29的博客 171

pat 甲级 A1078 Hashing

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 beH(key)=key%TSizewhereTSizeis the maximum size of the hash table. Qua...

m0_51711089的博客 282

PAT甲级——A1078 Hashing

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(whereTS...

weixin_30632089的博客 120

PAT A1078 Hashing (25分)

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 题目描述 The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and outpu...

Rhao的博客 249

算法设计:散列 PAT1078A Hashing

散列基本概念 参照以下书籍及链接掌握散列知识: 《算法笔记》胡凡 曾磊 P106-P111:散列的粗浅理解(适合小白) 《数据结构》(第2版) 陈越 P166-P197:比较系统地介绍了散列的知识,从应用实例到概念到例题讲解 Quadratic probing 二次探测散列法: 散列在搜索上性能较好,散列需要解决的两个问题主要是: 如何构造“好”的散列函数 如何制定冲突解决方案 二次探测散列法...

Enabler 234
上一篇: PAT B 1008 数组元素循环右移问题
下一篇: 中九低rank保研——浙软,沈自所,计算所,南计直博,复旦工程硕
Scienteresting
博客等级 码龄5年 58粉丝 7原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Scienteresting

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值