pat A1041 Be Unique

          pat A1041 Be Unique
题目:
Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1,10^4]. The first one who bets on a unique number wins. For example, if there are 7 people betting on { 5 31 5 88 67 88 17 }, then the second one who bets on 31 wins.

Input Specification:
Each input file contains one test case. Each case contains a line which begins with a positive integer N (≤10^5) and then followed by N bets. The numbers are separated by a space.

Output Specification:
For each test case, print the winning number in a line. If there is no winner, print None instead.

Sample Input 1:
7 5 31 5 88 67 88 17
Sample Output 1:
31

Sample Input 2:
5 888 666 666 888 888
Sample Output 2:
None

链接:pat A1041

#include <stdio.h>
int a[100010];
int hashTable[10010] = {};

int main(){
    int n;
    scanf("%d", &n);
    //读入所有数
    for(int i = 0; i < n; i++){
        //次序哈希--->线性变换
        scanf("%d", &a[i]);
        //次数哈希--->恒等变换
        hashTable[a[i]]++;
    }
    //判定
    int ans = -1;
    for(int i = 0; i < n; i++){
        if(hashTable[a[i]] == 1){
            ans = a[i];
            break;
        }
    }
    //输出
    if(ans == -1){
        printf("None");
    }else{
        printf("%d", ans);
    }
    return 0;
}

总结:

  1. 该题中有三个有用数据:
       ①输入的次序
       ②具体的数值
       ③各具体数值出现次数。
    现在要用合适的哈希函数和数组(hashTable)将这三个值连接起来。【从一个特别的角度理解哈希:将有用数据优美的连接起来】【而三个有用数据就需要连接两次】。
      对于次序,只需区别先后,并不需确切的次序,因此使用整数散列的直接定址法->线性变换,H(次序) = 次序-1,H(key1)被记录在数组a的键值中。
      对于具体数值,使用了整数散列的直接定址法->恒等变换,H(key2)被记录在数组hashTable的键值中。
  2. hash数组的长度总是 等于Max(H(Key)) + 10。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值