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, 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 (≤) 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个数字,要求输出第一个只出现一次的数字,如果不存在,输出None.

题解:

用类似打表的方法,统计每个数出现的次数。按输出顺序遍历,当有次数为1的数时,输出,没有就输出None。

AC代码:

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cstring>
using namespace std;
int a[10005];
int shu[100005];
int main()
{
    int n;
    cin>>n;
    memset(a,0,sizeof(a));
    for(int i=1;i<=n;i++){
        cin>>shu[i];
        a[shu[i]]++;
    }
    int f=0;
    for(int i=1;i<=n;i++){
        if(a[shu[i]]==1){
            cout<<shu[i];
            f=1;
            break;
        }
    }
    if(!f) cout<<"None";
    return 0;
}

原文地址:https://www.cnblogs.com/caiyishuai/p/11438337.html

时间: 2024-08-08 20:37:33

PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)的相关文章

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 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now given

PAT 甲级 1108 Finding Average (20分)

1108 Finding Average (20分) The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [−] and is a

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

题意: 输入一个正整数N(<=1e5),接下来输入N个正整数.输出第一个独特的数(N个数中没有第二个和他相等的),如果没有这样的数就输出"None". 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;multiset<int>st;int a[100007];int main(){ ios::sync_with_stdio(false); cin.tie(

PAT 甲级 1041 Be Unique

https://pintia.cn/problem-sets/994805342720868352/problems/994805444361437184 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

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 ar

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

【PAT甲级】1008 Elevator (20 分)

题意: 电梯初始状态停在第0层,给出电梯要接人的层数和层序号,计算接到所有人需要的时间,接完人后电梯无需回到1层(1层不是0层).电梯上升一层需要6秒,下降一层需要4秒,接人停留时间为5秒. 代码: #include<bits/stdc++.h> using namespace std; int a[100007]; int main(){ int n; cin>>n; int ans=0; for(int i=1;i<=n;++i){ cin>>a[i]; if

【PAT甲级】1077 Kuchiguse (20 分)(cin.ignore()吃掉输入n以后的回车接着用getine(cin,s[i])输入N行字符串)

题意: 输入一个正整数N(<=100),接着输入N行字符串.输出N行字符串的最长公共后缀,否则输出nai. 代码: #include<bits/stdc++.h>using namespace std;string s[107];int length[107];char ans[307];int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin>>n; cin.igno