跳到正文

个人记录

桶cyx

chenyixin0630约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年6月2日
篇幅
约 1 分钟

版本

  1. v1当前

桶

本质上就是数组 具体用法 排序、去重

C++
for(int i=1;i<=n;i++) {
int x;
cnt[x]++; }
//
//
//
//
for(int i=最小值;i<=最小值;i++){
if(cnt[x]==0)ans++;
}

错误原因:不知道怎么去重

正确代码

C++
#include<bits/stdc++.h>
using namespace std;
int cnt[1010];
int main (){
	int a,ans=0;
	cin>>a;
	for(int i=0;i<a;i++){
		int x;
		cin>>x;
		if(cnt[x]==0)ans++;
		cnt[x]++;
	}
	cout<<ans<<endl;
	for(int i=0;i<1000;i++){
		if(cnt[i]>0){
			cout<<i<<" ";
		}
	}
	return 0;
}
原文