快读小知识

文章展示了如何使用C++的快读模板读取输入,并结合unordered_map实现哈希表,以解决洛谷在线判题系统中的《JLOI2011不重复数字》问题。代码通过遍历输入,统计每个数字出现的次数,当数字首次出现时打印出来。
摘要由CSDN通过智能技术生成

这是快读模板:

int read() {
    char c = getchar();
    int x = 0, f = 1;
    for (; !isdigit(c); c = getchar())if (c == '-')f = -1;
    for (; isdigit(c); c = getchar())x = x * 10 + c - 48;
    return x * f;
}

哈希表创建:unordered_map<int, long long>a;

这次以洛谷《P4305 [JLOI2011]不重复数字》为例。

#include<bits/stdc++.h>
using namespace std;
long long t, n, tmp;
unordered_map<int, long long>a;//哈希表
int read() {//快读模板
	char c = getchar();
	int x = 0, f = 1;
	for (; !isdigit(c); c = getchar())if (c == '-')f = -1;
	for (; isdigit(c); c = getchar())x = x * 10 + c - 48;
	return x * f;
}
int main() {
	t=read();
	for(int i=1;i<=t;i++){
		a.clear();
		n=read();
		for(int i=1;i<=n;i++){
			tmp=read();
			a[tmp]++;
			if(a[tmp]==1)cout<<tmp<<' ';
		}
		cout<<endl;
	}
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值