题目

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

题目解读

给出N个正整数,找出第一个只出现了一次的数字,比如 5 31 5 88 67 88 1731,67,17都只出现了一次,但是31是第一个,所以输出31;如果没有唯一的数字,输出 None

思路很简单:利用一个整型数组统计每个数字出现的次数,找出第一个次数为1的数字并输出。

因为这些数字本身在输入中是无序的,因此不能直接用数字做下标,次数做值,这样会导致结果错误,比如上面那个例子 5 31 5 88 67 88 17,若用数字本身做下标,17会排在前面,最后会输出17.

因此设计两个数组num[]保存出现按顺序的这些数字,count[]保存这些数字出现的次数,最后只需要这样遍历:

    // 判断第一个只出现了一次的数字
for(int i = 0; i < n; i++) {
if(count[num[i]] == 1) {
printf("%d", num[i]);
return 0;
}
}

num[]本身按顺序读取输入并存储保证了数字的有序性。

完整代码

#include <cstdio>
using namespace std; int num[100000], count[100000]; int main() {
int n;
scanf("%d", &n);
int x;
for(int i = 0; i < n; i++) {
// 当前数字
scanf("%d", &num[i]);
// 当前数字出现的次数
count[num[i]]++;
}
// 判断第一个只出现了一次的数字
for(int i = 0; i < n; i++) {
if(count[num[i]] == 1) {
printf("%d", num[i]);
return 0;
}
}
// 都重复,输出 None
printf("None");
return 0;
}

PAT 1041 Be Unique (20分)利用数组找出只出现一次的数字的更多相关文章

  1. PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...

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

    1041 Be Unique (20 分)   Being unique is so important to people on Mars that even their lottery is de ...

  3. 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. ...

  4. 【PAT甲级】1041 Be Unique (20 分)(多重集)

    题意: 输入一个正整数N(<=1e5),接下来输入N个正整数.输出第一个独特的数(N个数中没有第二个和他相等的),如果没有这样的数就输出"None". AAAAAccepte ...

  5. 1041 Be Unique (20分)(水)

    Being unique is so important to people on Mars that even their lottery is designed in a unique way. ...

  6. PAT甲 1041. Be Unique (20) 2016-09-09 23:14 33人阅读 评论(0) 收藏

    1041. Be Unique (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Being uniqu ...

  7. 一起来刷《剑指Offer》——不修改数组找出重复的数字(思路及Python实现)

    数组中重复的数字 在上一篇博客中<剑指Offer>-- 题目一:找出数组中重复的数字(Python多种方法实现)中,其实能发现这类题目的关键就是一边遍历数组一边查满足条件的元素. 然后我们 ...

  8. 【Java】 剑指offer(2) 不修改数组找出重复的数字

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个长度为n+1的数组里的所有数字都在1到n的范围内,所以数组中至少 ...

  9. 《剑指offer》第五十六题(数组中唯一只出现一次的数字)

    // 面试题56(二):数组中唯一只出现一次的数字 // 题目:在一个数组中除了一个数字只出现一次之外,其他数字都出现了三次.请 // 找出那个吃出现一次的数字. #include <iostr ...

随机推荐

  1. 总结hashMap和hashtable

     在这里帮大家总结一下hashMap和hashtable方面的知识点吧: 1.  关于HashMap的一些说法:  a)  HashMap实际上是一个“链表散列”的数据结构,即数组和链表的结合体.Ha ...

  2. mysql 优化(包含sql语句的书写)

    http://blog.chinaunix.net/uid-11640640-id-3426908.html  mysql性能优化-慢查询分析.优化索引和配置 2012-11-30 15:18:42 ...

  3. Golang源码学习:使用gdb调试探究Golang函数调用栈结构

    本文所使用的golang为1.14,gdb为8.1. 一直以来对于函数调用都仅限于函数调用栈这个概念上,但对于其中的详细结构却了解不多.所以用gdb调试一个简单的例子,一探究竟. 函数调用栈的结构(以 ...

  4. 简说Spring中的资源加载

    声明: 本文若有 任何纰漏.错误,请不吝指正!谢谢! 问题描述 遇到一个关于资源加载的问题,因此简单的记录一下,对Spring资源加载也做一个记录. 问题起因是使用了@PropertySource来进 ...

  5. 前端开发Chrome调试工具

    Chrome浏览器提供了一个非常好用的调试工具,可以用来调试我们的HTML结构和CSS样式. 1.的打开调试工具 打开Chrome浏览器,按下F12键或点击页面空白,点击检查 2.使用调试工具 (1) ...

  6. Java:成员变量、局部变量和静态变量

    梳理一下: 根据定义变量位置的不同,可以将变量分成两大类:成员变量和局部变量. 成员变量(俗称全局变量):在类里定义的变量.又分为实例变量和类变量(也成为静态变量). 实例变量:不以static修饰, ...

  7. Spring_基于配置文件的方式配置AOP

    applicationContext-xml.xml <?xml version="1.0" encoding="UTF-8"?> <bean ...

  8. 逃离CSDN

    2012年2月18日,从CSDN搬家到这里.对于搬家的理由,总觉得该说点什么.我是在2012年1月29日又开始写博客的,在此之前我的CSDN博客闲置了10个月.从1月29日开始,不到1个月的时间建立了 ...

  9. STM32串口DMA接收数据错位——暴力解决方法

    背景:两片STM32通过串口通信,为了减小CPU负担,采用DMA进行通信,发送端为STM32F103C8T6,接收端为STM32F407VET6.在调试的过程中发现,一直出现数据错位的问题,接收端尝试 ...

  10. ms-setting是什么

    ms-settings 遇到了两个问题,记录一下 1)windows桌面右键菜单-->显示设置或者个性化-->报错:ms-settings:personalization-backgrou ...