zoukankan      html  css  js  c++  java
  • 题解 P4325 【[COCI2006-2007#1] Modulo】

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

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

    时间复杂度是O(log2n)O(log2{n})

    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;
    }
    
    

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

    我们知道c++c++中有一个函数叫uniqueunique,时间复杂度是O(log2n)O(log2{n})

    所以亮codecode

    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;
    }
    
    

    33种方法 哈希

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

    #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;
    }
    
    

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

  • 相关阅读:
    maven 配置
    如何快速在当前目录打开cmd命令提示符
    IntelliJ IDEA手工安python装插件方法
    Ubuntu14.04安装build-essential失败,包依赖问题如何解决?
    Xshell的telnet简化登录
    spring注入简记
    eclipse 技巧
    Linq 等式运算符:SequenceEqual(转载)
    一个可定制的图标网站
    EF 批量增删改 EntityFramework.Extensions
  • 原文地址:https://www.cnblogs.com/zhaohaikun/p/12817024.html
Copyright © 2011-2022 走看看