【FOJ】Problem 1001 Duplicate Pair

Problem 1001 Duplicate Pair.

题意

长度为n(1<n<=10^6)的数组,返回在数组中出现了两次的值。

思路

int一个整形数组arr[1000000]
每次输入值x,arr[x]的值加1
判断arr[x]的值是否为2,2则输出

笔记

  • 每组数据要初始化数组为0
  • 一组数据中可能有不止一个符合条件的值

代码

#include<stdio.h>
#include<string.h>

int arr[1000000];

int main(){
	int n;
	int temp;
	while(scanf("%d", &n) != EOF){
		memset(arr, 0, sizeof(arr));
		for(int i=0; i<n; i++){
			scanf("%d", &temp);
			arr[temp] += 1;
			if(arr[temp] == 2)
				printf("%d\n", temp);
		}
	}
	return 0;
}
发布了28 篇原创文章 · 获赞 0 · 访问量 339

猜你喜欢

转载自blog.csdn.net/qq_44531167/article/details/105077558