PAT Advanced 1041 Be Unique (20) [Hash散列]

题目

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

题目分析

  1. 定义数组ns[N],存放输入数字
  2. 定义数组ts[10001],记录输入数字出现次数
    • 大小为10001,(题目已知:输入数字取值范围[1, 10^4])
    • 数组下标--输入数字
    • 数组元素--输入数字出现次数
  3. 遍历输入数字(ns数组),最早出现次数==1(表明是唯一数),打印退出

易错点

  1. 若没有唯一数时,打印None,注意None后不需要加"\n"就可以AC

Code

Code 01

#include <iostream>
//#include <>
using namespace std;
int main(int argc, char * argv[]){
    int N,m;
    scanf("%d",&N);
    int ns[N];
    int ts[10001]={0};
    for(int i=0;i<N;i++){
        scanf("%d",&ns[i]);
        ts[ns[i]]++;
    }
    bool flag = false;
    for(int i=0;i<N;i++){
        if(ts[ns[i]]==1){
            printf("%d",ns[i]);
            flag = true;
            break;
        }
    }
    if(!flag)printf("None");
    return 0;
}

原文地址:https://www.cnblogs.com/houzm/p/12238454.html

时间: 2024-08-01 14:45:14

PAT Advanced 1041 Be Unique (20) [Hash散列]的相关文章

PAT Advanced 1050 String Subtraction (20) [Hash散列]

题目 Given two strings S1 and S2, S = S1 – S2 is defined to be the remaining string afer taking all the characters in S2 from S1. Your task is simply to calculate S1 – S2 for any given strings. However, it might not be that simple to do it fast. Input

PAT Advanced 1050 Broken Keyboard (20) [Hash散列]

题目 On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen.Now given a string that you are supposed to type, and the string that you actually type out,

PAT Advanced 1041 Be Unique (20 分)

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]. The first one who bets on a unique number wins. For example, if there are 7 peopl

PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)

1041 Be Unique (20 分) 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]. The first one who bets on a unique number wins. For example

PAT Advanced 1092 To Buy or Not to Buy (20) [Hash散列]

题目 Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the owner of the shop would only sell the strings in whole pieces. Hence Eva must c

PAT:1041. Be Unique (20) AC “段错误:可能是数组开的不够大”

#include<stdio.h> #include<string.h> int harsh[10066]; int arr[100066]; int main() { memset(harsh,0,sizeof(harsh)); memset(arr,0,sizeof(arr)); int n; scanf("%d",&n); for(int i=0 ; i<n ; ++i) { int tmp; scanf("%d",&am

1041. Be Unique (20)【水题】——PAT (Advanced Level) Practise

题目信息 1041. Be Unique (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B 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, 104]. The first on

[Hash散列] 7-1 统计工龄 (15分)

给定公司N名员工的工龄,要求按工龄增序输出每个工龄段有多少员工. 输入格式: 输入首先给出正整数N(N≤105),即员工总人数:随后给出N个整数,即每个员工的工龄,范围在[0, 50]. 输出格式: 按工龄的递增顺序输出每个工龄的员工个数,格式为:“工龄:人数”.每项占一行.如果人数为0则不输出该项. 输入样例: 8 10 2 0 5 7 2 5 2 输出样例: 0:1 2:3 5:2 7:1 10:1 思路:哈希散列存进一维数组,然后就输出吧! 1 #include<iostream> 2

2020年2月24日09:06:11,Hash散列

问题描述 /** 有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,住址..),当输入该员工的id时,* 要求查找到该员工的所有信息.* ?要求: 1)不使用数据库,,速度越快越好=>哈希表(散列)* 2)添加时,保证按照id从低到高插入[课后思考:如果id不是从低到高插入,但要求各条链表仍是从低到高,怎么解决?]* 3)使用链表来实现哈希表, 该链表不带表头** */ 代码实现 package day0223 /* * 有一个公司,当有新的员工来报道时,要求将该员工的