1083 是否存在相等的差 (20 分)

思路

  • 使用数组求差值,并统计次数。

注意

  • 输出只需要输出重复的,出现一次不叫重复,我像个弱智一样反复读了好多遍题目才懂。

2021.04.13

// 8
// 3 5 8 6 2 1 4 7
// 1 2 3 4 5 6 7 8
// 2 3 5 2 3 5 3 1

// 5 2
// 3 3
// 2 2

#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e4+5;
int hashmap[N];
int main(){
	
	memset(hashmap, 0, sizeof hashmap);
	int n, x;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d", &x);
		hashmap[abs(x - i)]++;
	}
	// 输出 1. 从大到小 2. 重复的元素
	for(int i = n; i >= 0; i--){
		if(hashmap[i] > 1){
			printf("%d %d\n", i, hashmap[i]);
		}
	} 
	return 0;
}

旧版本

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int hash[10005]={0};
int arr[10005];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&arr[i]);
		hash[abs(arr[i]-i)]++;
	}
	
	for(int i=n-1;i>=0;i--){
		if(hash[i]>1){
			printf("%d %d\n",i,hash[i]);
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值