5 条题解

  • 2
    @ 2022-12-25 16:20:24
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
    	set<int>st;
    	int n, x;
    	cin >> n;
    	for (int i = 1; i <= n; i++)
    	{
    		cin >> x;
    		st.insert(x);
    	}
    	cout << st.size() << endl;
    	for (set<int>::iterator i = st.begin(); i != st.end(); i++)
        {
    		cout << *i << " ";
        }
    	return 0;
    }
    
    • 1
      @ 2023-11-8 16:36:07
      #include<bits/stdc++.h>
      using namespace std;
      int main(){
      	int n;
      	cin>>n;
      	set<int> s;
      	int a;//input value
      	for(int i=0;i<n;i++){
      		cin>>a;
      		s.insert(a);
      	}
      	int ans=0;
      	for(set<int>::iterator it=s.begin();it!=s.end();it++){
      		ans++;//s.size()
      	}
      	cout<<ans<<endl;
      	for(set<int>::iterator it=s.begin();it!=s.end();it++){
      		cout<<*it<<" ";
      	}
      	return 0;
      }
      
      • 0
        @ 2024-3-3 10:01:21

        我发现它更去除重复数字一样

        • 0
          @ 2023-8-5 22:23:45

          set&&数组下标

          1.set 这道题去重+排序用set肯定是最简单的 //

          #include<bits/stdc++.h>
          using namespace std;
          set <int> a;
          int main(){
              int n;
              cin>>n;
              for(int i=1;i<=n;i++){
                  int b;
                  cin>>b;
                  a.insert(b);
              }
              cout<<a.size()<<endl;
              for(set<int> :: iterator it=a.begin();it!=a.end();it++){//迭代器
                  cout<<*it<<" ";
              }
          return 0;
          }
          

          2.数组下标法 数组下标法做这道题也好做,就是要输出长度有点小麻烦。

          #include<bits/stdc++.h>
          using namespace std;
          const int N=1e6+5;
          int a[105],b[N],c[N],size,maxx;
          int main(){
              int n;
              cin>>n;
              for(int i=1;i<=n;i++){
                  scanf("%d",&a[i]);
                  maxx=max(maxx,a[i]);//maxx是输入的a[i]中的最大值,数组下标法里b[i]最大非空位就是最大的a[i],所以下面一个for要i<=maxx来保证能遍历到b里面所有的1
                  b[a[i]]=1;//去重其实就在这里实现了,每次输入同样的数时,都会把b[a[i]]=1,最后输出有1的就行
              }
              for(int i=1;i<=maxx;i++){//c数组的意义是下面一个for为了去重,要将b中已经统计的数归零,而下面还要输出b[a[i]],所以用一个c数组来输出
                  c[i]=b[i];
              }
              for(int i=1;i<=n;i++){//这个for用来统计输出的个数
                  if(b[a[i]]==1){
                      size++;
                      b[a[i]]=0;
                  }
              }
              sort(a+1,a+n+1);//排序
              cout<<size<<endl;
              for(int i=1;i<=n;i++){//这个for用来输出
                  if(c[a[i]]==1){//因为a里没有的数都是0,所以要判断为1再输出
                      cout<<a[i]<<" ";
                      c[a[i]]=0;
                  }
              }
          return 0;
          }
          

          当然这不是全部的方法,欢迎补充和讨论,大蛇们不喜勿喷

          • 0
            @ 2023-6-29 20:34:41

            set

            • 1

            信息

            ID
            761
            时间
            1000ms
            内存
            16MiB
            难度
            2
            标签
            递交数
            87
            已通过
            56
            上传者