PTA甲級——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,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
題目大意:給出序列的長度和一個數字序列,輸出序列中第一個只出現一次的數字,如果不存在則輸出None。
類似於桶排序,可以使用一個陣列直接記錄每個數字出出現的次數,(如果數字過大,會造成大量的記憶體浪費,可以使用雜湊表),然後順序遍歷序列查詢。(如果不想要一個相同的數字被多次和查詢,可以使用set來優化)
程式碼實現:
#include <iostream>
using namespace std;
int times[10005], num[10005];
int main()
{
int t, temp, bit, sign = 0, cnt = 0;
cin >> t;
while (t--)
{
cin >> temp;
if (times[temp] == 0) { num[cnt++] = temp; times[temp]++; }
else times[temp]++;
}
for (int i = 0; i < cnt; i++)
{
if (times[num[i]] == 1)
{
cout << num[i];
sign = 1;
break;
}
}
if (!sign) cout << "none";
return 0;
}
相關文章
- PTA甲級 1076 Forwards on Weibo (30分)Forward
- 【PTA甲級、C++簡單解答】1001 A+B Format (20分)C++ORM
- PAT甲級1032 Sharing
- 1021 Deepest Root(甲級)
- PAT甲級1030 Travel Plan
- 浙大PAT甲級考試
- PTA乙級 1004 成績排名
- PAT甲級1023 Have Fun with Number
- PAT 甲級 1152 Google Recruitment (20分)GoUI
- 20年春季甲級pat考試
- PAT甲級-1015. Reversible Primes (20)
- Unique Array
- unique 用法
- PAT甲級1126~1130|C++實現C++
- PAT甲級-1014. Waiting in Line (30)(模擬)AI
- pta
- PAT甲級真題1069 數字黑洞(巧妙解法)
- PAT甲級考試題庫題目分類
- 【PAT甲級A1084】Broken Keyboard (20分)(c++)C++
- SQL Server Unique ConstratintsSQLServer
- PTA乙級_1024 科學計數法 (20分)_pythonPython
- 2024 秋季PAT認證甲級(題解A1-A4)
- 【PAT甲級A1038】Recover the Smallest Number (30分)(c++)C++
- PAT甲級1122 Hamiltonian Cycle (25分)|C++實現C++
- PAT甲級1154 Vertex Coloring (25分)|C++實現C++
- PAT甲級-1140. Look-and-say Sequence (20)(模擬)
- PTA出租
- 夕甲甲——孔乙己之C++版C++
- 2021.9.12週六PAT甲級考試覆盤與總結
- PAT甲級1110 Complete Binary Tree (25分)|C++實現C++
- 19年春季第二題 PAT甲級 1157 Anniversary(25 分)
- [PTA]day 7
- [PTA]day 9
- [PTA]day 2
- [PTA]day 5
- [PTA]day 3
- [PTA]day 1
- pta檢索