PAT 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,104 ]. 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 (≤105 ) 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 17,31,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分)利用数组找出只出现一次的数字的更多相关文章
- 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 ...
- PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)
1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is de ...
- 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. ...
- 【PAT甲级】1041 Be Unique (20 分)(多重集)
题意: 输入一个正整数N(<=1e5),接下来输入N个正整数.输出第一个独特的数(N个数中没有第二个和他相等的),如果没有这样的数就输出"None". AAAAAccepte ...
- 1041 Be Unique (20分)(水)
Being unique is so important to people on Mars that even their lottery is designed in a unique way. ...
- 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 ...
- 一起来刷《剑指Offer》——不修改数组找出重复的数字(思路及Python实现)
数组中重复的数字 在上一篇博客中<剑指Offer>-- 题目一:找出数组中重复的数字(Python多种方法实现)中,其实能发现这类题目的关键就是一边遍历数组一边查满足条件的元素. 然后我们 ...
- 【Java】 剑指offer(2) 不修改数组找出重复的数字
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个长度为n+1的数组里的所有数字都在1到n的范围内,所以数组中至少 ...
- 《剑指offer》第五十六题(数组中唯一只出现一次的数字)
// 面试题56(二):数组中唯一只出现一次的数字 // 题目:在一个数组中除了一个数字只出现一次之外,其他数字都出现了三次.请 // 找出那个吃出现一次的数字. #include <iostr ...
随机推荐
- 初涉WebGL
之前一直在捣鼓Vue和React栈,对组件化架构项目有了些理解和体会.今天尝尝WebGL,当然,并不打算现在深入,只是略作了解,我知道这个坑很深. js的图形库.3d库也有好几款比较流行的,如游戏开发 ...
- lonic常用组件之五------按钮
一.Ionic常用组件之五------按钮 <ion-button color="主题色" size="small/large" expand=& ...
- Linux 下三种提高工作效率的文件处理技巧
Linux 下三种提高工作效率的文件处理技巧 在 Linux 下工作,打交道最多的就是文件了,毕竟 Linux 下工作一切皆文件嘛.Linux 也为大家提供了多种用于处理文件的命令,合理使用这些命令可 ...
- 17-4 delete-truncate语句-练习
--删除数据语句: --delete from 表名 where ... --delete语句如果不加where条件,表示将表中所有的数据都删除,加where条件后,会按照where条件进行删除. - ...
- Jmeter基础-HTTP请求
启动Jmeter 打开jmeter/bin文件/jmeter.bat(Windows执行文件)文件,就可以启动jmeter了 1.创建测试计划 启动后默认有一个TestPlan(测试计划),可修改其名 ...
- Kafka SSL安装与配置
1.概述 最近有同学咨询说,Kafka的SSL安全认证如何安装与使用?今天笔者将通过以下几个方面来介绍Kafka的SSL: Kafka 权限介绍 Kafka SSL的安装与使用 Kafka Eagle ...
- MyBatis——Mapped Statements collection does not contain value for XXX
报错信息: Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql. ...
- 一、CentOS6.8安装MySQL5.6
一.官网下载rpm安装包 https://dev.mysql.com/downloads/ 版本选中如图中红色框 二.卸载旧mysql 1.检查是否安装有mysql rpm -qa | grep -i ...
- [Objective-C] 007_Foundation框架之NSString与NSMutableString
在Cocoa Foundation中的NSString和NSMutableString类,为我们提供了Unicode字符串的支持,NSString和NSMutableString类最大的区别是:NSS ...
- angularjs 路由切换回到顶部
angularjs路由切换 页面不会回到顶部 问题: 在angularjs中 ui-sref或者$state.go(),通过路由切换页面,发现新打开的路由页面仍然停留在上一次的路由页面访问的位置. ...