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
题目分析
已知一系列数,找到第一个不重复的数字
解题思路
- 定义数组ns[N],存放输入数字
- 定义数组ts[10001],记录输入数字出现次数
- 大小为10001,(题目已知:输入数字取值范围[1, 10^4])
- 数组下标--输入数字
- 数组元素--输入数字出现次数
- 遍历输入数字(ns数组),最早出现次数==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;
}
PAT Advanced 1041 Be Unique (20) [Hash散列]的更多相关文章
- PAT Advanced 1084 Broken Keyboard (20) [Hash散列]
题目 On a broken keyboard, some of the keys are worn out. So when you type some sentences, the charact ...
- 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 th ...
- 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 Advanced 1048 Find Coins (25) [Hash散列]
题目 Eva loves to collect coins from all over the universe, including some other planets like Mars. On ...
- PAT Advanced 1134 Vertex Cover (25) [hash散列]
题目 A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at ...
- PAT Basic 1047 编程团体赛(20) [Hash散列]
题目 编程团体赛的规则为:每个参赛队由若⼲队员组成:所有队员独⽴⽐赛:参赛队的成绩为所有队员的成绩和:成绩最⾼的队获胜.现给定所有队员的⽐赛成绩,请你编写程序找出冠军队. 输⼊格式: 输⼊第⼀⾏给出⼀ ...
- 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 ...
- 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 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 ...
随机推荐
- HDU 3397 线段树 双懒惰标记
这个是去年遗留历史问题,之前思路混乱,搞了好多发都是WA,就没做了 自从上次做了大白书上那个双重懒惰标记的题目,做这个就思路很清晰了 跟上次大白上那个差不多,这个也是有一个sets标记,代表这个区间全 ...
- winfrom窗体的透明度
在VS中创建一个Winform项目,其默认的窗体名称为 Form1. 在VS设计界面中对 Form1 的 Opacity 属性值设置为 50%. 没错,就这样就可以了. 方法2: ...
- Tomcat JDK MySql 安装配置
Tomcat 7绿色版指定jdk并注册服务 https://blog.csdn.net/weixin_43976019/article/details/89386171 例如:service.b ...
- c++程序—switch分支
#include<iostream> using namespace std; #include<string> int main() { //多元分支 cout <&l ...
- java课程之团队开发冲刺阶段2.3
一.总结昨天进度 1.完成整合功能 二.遇到的问题 1.在整合的过程中,总是发现在switch开关提醒了两次,后来发现是因为我使用了setChecked方法,而这个方法触发onCheckedChang ...
- 转: 十大Intellij IDEA快捷键
Intellij IDEA中有很多快捷键让人爱不释手,stackoverflow上也有一些有趣的讨论.每个人都有自己的最爱,想排出个理想的榜单还真是困难.以前也整理过Intellij的快捷键,这次就按 ...
- [题解] LuoguP4389 付公主的背包
这个题太神辣- 暴力背包就能获得\(30\)分的好成绩...... \(60\)分不知道咋搞..... 所以直接看\(100\)分吧\(QwQ\) 用一点生成函数的套路,对于一个体积为\(v\)的物品 ...
- 在mysql中计算百分比
通过查找资料,得到了如下解决方法: 用到了concat()和left() 两个函数 1.CONCAT(str1,str2,...) 返回来自于参数连结的字符串.如果任何参数是NULL, 返回NULL. ...
- python np array转json
np array转json import numpy as np import codecs, json a = np.arange().reshape(,) # a by array b = a.t ...
- 可重入排他锁ReentrantLock源码浅析
1.引子 "ReentrantLock"单词中的“Reentrant”就是“重入”的意思,正如其名,ReentrantLock是一个支持重入的排他锁,即同一个线程中可以多次获得同步 ...