C++ A-B Problem

Question:
Given a string of numbers and a number C, it is required to calculate the number of all pairs of AB=C. (Note: the same number in different positions counts as different number pairs) where A and B must belong to this string of numbers.
Input: The
first line includes two non-negative integers N and C, separated by spaces. The second line has N (N<=200000) integers, separated by spaces, as the string of numbers required to be processed.
Output:
output one line, indicating the number of all pairs satisfying AB=C contained in the string (assuming that each input will have a result of at least 1)
Input example:
4 1
1 1 2 3
Output:
3

#include<iostream>
#include<map>//导入映射库 
using namespace std;
map<long,int> m;//长整型数据作键,整型作值,创建映射
int n;
long c,num[200005];//由于数据过大
int main(){
    
    
	while(scanf("%d%ld",&n,&c)  !=EOF){
    
    
		int ans=0,i=n;
		while(i--){
    
    
			cin>>num[i];//依填输入N个数据  
			m[num[i]]++;//统计每个数出现的次数!
		}
		i=n;//千万别忘了更新 i 的值 
		if(c>0)//仅一个分支时可以不用大括号,简化代码
			while(i--)
				ans+=m[num[i]+c];
		else
			while(i--)
				ans=ans+m[num[i]+c]-1;//去掉自己
		cout<<ans<<endl;
	}
	return 0; 
} 

Here is mainly to familiarize yourself with the operation of the map.
Come on! ! !

Guess you like

Origin blog.csdn.net/interestingddd/article/details/113766500