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

代码

#include <iostream>
#include <map>
#include <math.h>

using namespace std;

int main() {
	
	int n;
	cin>>n;
	map<int, int> m;
	for(int i=1; i<=n; i++){
		int t;
		cin>>t;
		int sub = abs(t-i);
		m[sub]++;
	}
	for(int i=n; i>=0; i--){
		if(m[i]>1){
			cout<<i<<" "<<m[i]<<endl;
		}
	}
	
	return 0;
}

注解

1、用map存储数字跟次数的映射关系。很常用的方法,map是个非常有力的武器,要经常记得使用!

结果

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

相关文章:

  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
  • 2021-09-25
  • 2021-07-17
  • 2021-07-19
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-17
  • 2022-01-02
  • 2021-08-04
  • 2021-06-26
  • 2021-10-23
  • 2022-01-31
相关资源
相似解决方案