zoukankan      html  css  js  c++  java
  • C

    C - Buying A House

    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, thenai 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.

    Input

    The first line contains three integers nm, and k (2 ≤ n ≤ 100, 1 ≤ m ≤ n1 ≤ 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.

    Output

    Print one integer — the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy.

    Example

    Input
    5 1 20
    0 27 32 21 19
    Output
    40
    Input
    7 3 50
    62 0 0 0 99 33 22
    Output
    30
    Input
    10 5 100
    1 0 1 0 0 0 0 0 1 1
    Output
    20
    题意:第一行输入3个数,房子的数量n,小女孩家的房子编号m,你拥有的钱k。
       第二行输入n个数,代表房子的价格,0表示你不能购买。
       要求你买一个房子使得你离小女孩家最近。
    题解:暴力计算你所能买的起的所有房子到小女孩家的距离,输出最小值。
    代码:
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cstdlib>
    #include<cmath>
    using namespace std;
    int main()
    {
        int i,n,m,k,a[105],ans[105];
        while(cin>>n>>m>>k){
                for(i=1;i<=n;i++)
                    ans[i]=999999999;
            for(i=1;i<=n;i++)
                cin>>a[i];
            for(i=1;i<=n;i++)
            {
                if(a[i]!=0&&i!=m)
                {
                    if(a[i]<=k)
                        ans[i]=fabs(i-m)*10;
                }
            }
            cout<<*min_element(ans+1,ans+1+n)<<endl;
        }
    }
    
    
    
    
    
  • 相关阅读:
    Flask 入门(十二)
    Flask 入门(特别篇)
    Flask 入门 (十一)
    Flask 入门(十)
    Flask 入门(九)
    [20171031]markhot.txt
    [20170927]关于hugepages.txt
    [20170927]hugepages与内核参数nr_overcommit_hugepages.txt
    [20170916]sqlplus set array最小2补充.txt
    [20170914]tnsnames.ora的管理.txt
  • 原文地址:https://www.cnblogs.com/GXXX/p/6813773.html
Copyright © 2011-2022 走看看