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.
Sample Input
4 2 6 8
5
1
Sample Output
6 4
题意
输入一行数,以换行结尾,每个数后面都跟一个空格(最后一个数也有)
输入a,b,判断距离a小于等于b有哪些点,从大到小输出这些邻居点即可
思路
主要注意输入就可以,题目很简单,因为我没读出来输入的最后一个也有空格,所以一直WA
代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long b[500010],s[500010];
bool cmp(long long x,long long y)
{
return x>y;
}
int main()
{
int j=0;
char a;
int f1=0;
long long ans=0;
while(~scanf("%c",&a))
{
if(a=='\n')
{
/* if(f1)
b[++j]=-ans;
else
b[++j]=ans;*/
break;
}
if(a!=' ')
{
if(a=='-')
f1=1;
else
ans=ans*10+(a-'0');
}
else if(a==' ')
{
if(f1==0)
b[++j]=ans;
else
b[++j]=-ans;
ans=0;
f1=0;
}
}
long long c,d;
scanf("%lld",&c);
scanf("%lld",&d);
long long e,f;
e=c-d;
f=c+d;
int k=0;
for(int i=1; i<=j; i++)
{
if(b[i]>=e&&b[i]<=f)
s[++k]=b[i];
}
if(k==0)
printf("\n");
else
{
sort(s+1,s+1+k,cmp);
for(int i=1; i<=k; i++)
printf("%lld ",s[i]);
}
return 0;
}