1 条题解

  • 2
    @ 2025-1-31 18:44:36

    悲!这个人机题我竟只拿了40分!

    这道题我在盐城大丰高级中学的考场上用了5分钟就写完了(我竟在考场上没有回来检查一下),不出所料的拿了40分??!!!

    我已憨到直接输出52-n,都不考虑去重的。。。

    考场代码:

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
        freopen("poker.in","r",stdin);
    	freopen("poker.out","w",stdout);
    	int n;
    	cin>>n;
    	string s[10005];
    	for(int i=1;i<=n;i++){
    		cin>>s[i];
    	}
    	cout<<52-n;
        fclose(stdin);
        fclose(stdout);
    	return 0;
    }
    
    

    这道题我将用两种STL的方法来解答。

    解法一:

    运用STL中的unique,unique可以对一个有序的序列去重,并且将重复的放在末尾,unique会返回去重后这个序列的末指针。 即把所有牌sort一遍用个unique就可以了。

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
        freopen("poker.in","r",stdin);
    	freopen("poker.out","w",stdout);
    	int n;
    	cin>>n;
    	string s[10005];
    	for(int i=1;i<=n;i++){
    		cin>>s[i];
    	}
        sort(s+1,s+n+1);
    	n=unique(s+1,s+n+1)-s-1;
    	cout<<52-n;
        fclose(stdin);
        fclose(stdout);
    	return 0;
    }
    
    

    解法二:

    运用STL中的set。set可以自动去重,剩下的就是不同的牌。

    #include <bits/stdc++.h>
    using namespace std;
    int main() {
        freopen("poker.in","r",stdin);
    	freopen("poker.out","w",stdout);
    	set <string> S;
    	int n;
        cin>>n;
    	for(int i=1;i<=n;i++){
    		string s;
    		cin>>s;
    		S.insert(s);
    	}
    	cout<<52-S.size();
        fclose(stdin);
        fclose(stdout);
    	return 0;
    }
    
    

    信息

    ID
    34937
    时间
    1000ms
    内存
    512MiB
    难度
    8
    标签
    递交数
    14
    已通过
    6
    上传者