Codeup100000582问题 C: Be Unique (20)

Codeup100000582问题 C: Be Unique (20)

题目描述:

Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1, 104]. The first one who bets on a unique number wins. For example, if there are 7 people betting on 5 31 5 88 67 88 17, then the second one who bets on 31 wins.

输入:

Each input file contains one test case. Each case contains a line which begins with a positive integer N (<=105) and then followed by N bets. The numbers are separated by a space.

输出:

For each test case, print the winning number in a line. If there is no winner, print “None” instead.

样例输入:

7 5 31 5 88 67 88 17
5 888 666 666 888 888

样例输出:

31
None

实现代码:

#include<cstdio>
#include<cstring>
const int maxn=10010;
int num[maxn];
int main(){
	int n;
	while(scanf("%d",&n)!=EOF){
		int* bets=new int[n];
		memset(num,0,sizeof(num));
		bool flag = false;
		for(int i=0;i<n;i++){
			scanf("%d",&bets[i]);
			num[bets[i]]++;
		}
		for(int i=0;i<n;i++){
			if(num[bets[i]]==1){
				printf("%d\n",bets[i]);
				flag = true;
				break;
			}
		}
		if(!flag){
			printf("None\n");
		}
		delete[] bets;
	}
	return 0;
} 

std::unique_ptr是C++11引入的智能指针,用于管理动态内存的生命周期。它提供了一种安全且方便的方式来分配和释放动态内存,避免了手动管理内存的繁琐和容易出错的问题。 要使用std::unique_ptr来开辟动态内存,可以按照以下步骤进行: 1. 包含头文件:首先需要包含<memory>头文件,因为std::unique_ptr是在该头文件中定义的。 2. 创建std::unique_ptr对象:使用std::unique_ptr来管理动态内存,需要创建一个std::unique_ptr对象,并将其初始化为指向动态内存的指针。例如: ```cpp std::unique_ptr<int> ptr(new int); ``` 这里创建了一个std::unique_ptr对象ptr,并将其初始化为指向一个int类型的动态内存。 3. 使用std::unique_ptr对象:可以像使用原始指针一样使用std::unique_ptr对象。例如,可以通过解引用操作符*来访问动态内存中的值,也可以使用箭头操作符->来访问动态内存中的成员。例如: ```cpp *ptr = 10; std::cout << *ptr << std::endl; ``` 这里将动态内存中的值设置为10,并输出该值。 4. 自动释放内存:当std::unique_ptr对象超出其作用域时,会自动释放所管理的动态内存。这意味着不需要手动调用delete来释放内存,从而避免了内存泄漏和悬空指针的问题。 总结一下,使用std::unique_ptr开辟动态内存的步骤是:包含头文件、创建std::unique_ptr对象并初始化为指向动态内存的指针、使用std::unique_ptr对象操作动态内存、自动释放内存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值