We have a set S of one-dimensional points.
Given a target point A, we would like to find the neighboring points of A in S. We consider two points are neighbors, if and only if they are within a distance of r.
Input
The first line lists all the coordinates of the points in S. Each coordinate (including the last one) is followed by a single space.
The second line is the coordinate of point A.
And the third line is the value of distance r.
You can assume that all the points in S have different coordinates, ∣S∣≤100,000, and all the values above are 16-bit integers.
Output
A single line prints the coordinates of A’s neighbors in S. The coordinates should be sorted in descending order, and each coordinate (including the last one) should be followed by a single space.
If the neighbourhood is empty, print an empty line.
input
4 2 6 8
5
1
output
6 4
恶心的输入:需要用EOF才能截至输入
自身也需要输出
#include<stdio.h>
#include<vector>
#include<map>
#include<string.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
vector<ll >s;
vector<ll >s11;
int main()
{
ll a;
while(EOF!=scanf("%lld",&a))
{
s.push_back(a);
}
for(int i=0;i<=s.size()-3;i++)
s11.push_back(s[i]);
ll s1=s[s.size()-2];
ll s2=s[s.size()-1];
sort(s11.begin(),s11.end());
ll flag=0;
for(ll i=s11.size()-1; i>=0; i--)
if(abs(s11[i]-s1)<=s2)
{
cout<<s11[i]<<" ";
flag=1;
}
if(flag==0)
cout<<endl;
}
做法二:map
#include<stdio.h>
#include<iostream>
#include<map>
#include<vector>
#define ll long long
using namespace std;
map<ll,ll > p;
vector<ll > s;
int main()
{
ll a;
while(EOF!=scanf("%lld",&a))
{
s.push_back(a);
}
for(ll i=0;i<=s.size()-3;i++)
{
p[s[i]]=1;
}
ll mid=s[s.size()-2];
ll qq=1;
a=s[s.size()-1];
for(ll i=mid+a;i>=mid-a;i--)
{
if(p[i]==1)
{
qq=0;
printf("%lld ",i);
}
}
if(qq==1)
cout<<endl;
}