题目描述:
Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us.
The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, …, house n. The village is also well-structured: house i and house i + 1 (1 ≤ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased.
You will be given n integers a1, a2, …, an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars.
As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush’s house to some house he can afford, to help him succeed in his love.
输入描述:
The first line contains three integers n, m, and k (2 ≤ n ≤ 100, 1 ≤ m ≤ n, 1 ≤ k ≤ 100) — the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively.
The second line contains n integers a1, a2, …, an (0 ≤ ai ≤ 100) — denoting the availability and the prices of the houses.
It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars.
输出描述:
Print one integer — the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy.
输入:
5 1 20
0 27 32 21 19
7 3 50
62 0 0 0 99 33 22
10 5 100
1 0 1 0 0 0 0 0 1 1
输出:
40
30
20
题意:
读入 n 个整数 a1, a2, …, an 表示每栋房子的价格。如果房子 i 已经被占有,也就是不能被购买,那么 ai 等于 0。否则房子 i 可以被购买,价格为 ai 。
现在Zane只有 k 元,他想知道他应该买哪所房子使他离他的爱人住的最近。
题解:
按题意模拟
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int a[1005];
int main(){
int n,m,k;
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
for(int i = 1; i <= n; i ++) scanf("%d",&a[i]);
int minn = 1005;
int t = 0;
for(int i = 1; i <= n; i ++){
if(a[i] != 0 && a[i] <= k){
minn = min(minn,abs(i - m));
}
}
int ans = 10 * minn;
printf("%d\n",ans);
}
return 0;
}