题解 P4325 【[COCI2006-2007#1] Modulo】

2 篇文章 0 订阅

1 1 1种方法 也是最暴力的一种

我们熟知, c + + c++ c++中的 s e t set set可以既去重,有排序,这题,我们可以用set来搞,虽然我们不需要排序的功能,但毕竟方便,一共是 10 10 10个数,所以暴力一点也没事

时间复杂度是 O ( log ⁡ 2 n ) O(\log2{n}) O(log2n)

c o d e : code: code:

#include <bits/stdc++.h>
using namespace std;
set<int>a;
int main(){
	for(int i=1;i<=10;i++){
		int x;
		cin>>x;
		a.insert(x%42);
	}cout<<a.size();
	return 0;
}

2 2 2种方法 用 c + + c++ c++中的函数实现

我们知道 c + + c++ c++中有一个函数叫 u n i q u e unique unique,时间复杂度是 O ( log ⁡ 2 n ) O(\log2{n}) O(log2n)

所以亮 c o d e code code

c o d e : code: code:

#include <bits/stdc++.h>
using namespace std;
int a[15];
int main(){
	for(int i=1;i<=10;i++)cin>>a[i];
	sort(a+1,a+n+1);
	int ans=unique(a+1,a+10+1)-a;
	cout<<ans;
	return 0;
}

3 3 3种方法 哈希

h h h数组记录这个数有没有存过

#include <bits/stdc++.h>
using namespace std;
int h[110],s;
int main(){
	for(int i=1;i<=10;i++){
		int x;
		cin>>x;
		if(++h[x%42]==1)s++;
	}
	cout<<s;
	return 0;
}

或者之后再扫一遍

#include <bits/stdc++.h>
using namespace std;
int h[110],s;
int main(){
	for(int i=1;i<=10;i++){
		int x;
		cin>>x;
		h[x%42]++;
	}
	for(int i=0;i<41;i++)//余数是从0到41
		if(h[i])s++;
	cout<<s;
	return 0;
}

介绍了这 3 3 3种方法,希望大家能点赞,欢迎评论!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值