题解 P4305 【[JLOI2011]不重复数字】

来一波用vector的最短代码题解

P4305 [JLOI2011]不重复数字

  1. 关于hash表的部分大家可以看一看其他的题解,我就不说了
  2. 不定长数组vector的几个基本用法:
    1. 定义: vector<数据类型> 数组名称
    2. 访问: a[pos]//访问a数组下标为pos的元素
    3. 尾部加入元素: a.push_back(x)
    4. 判断是否为空: a.empty()//空返回true,非空返回false
  3. 代码:
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
int n,hash_num=50021,t,temp;
vector<int> a[50022];
int main()
{
	ios::sync_with_stdio(false); //快速读入
	
	cin>>t;
	for(int i=1;i<=t;i++)
	{
		cin>>n;
		memset(a,0,sizeof(a));//重置hash表
		for(int j=1;j<=n;j++)
		{
			cin>>temp;//输入当前数据
			int hash=temp%hash_num;//hash过程
			bool pd=false;
			if(a[hash].empty()==false)
			{
				for(int k=0;k<a[hash].size();k++)
				{
					if(a[hash][k]==temp)//判断hash表中是否有当前元素
						pd=true;
				}
			} 
			else if(pd==false)
			{
				a[hash].push_back(temp);//如果hash表中没有,加入hash表并输出
				printf("%d ",temp);
			}
		}
		printf("
");
	}
}
原文地址:https://www.cnblogs.com/huaruoji/p/12076350.html