zoukankan      html  css  js  c++  java
  • Where is Vasya?

    Where is Vasya?

    Vasya stands in line with number of people p (including Vasya), but he doesn't know exactly which position he occupies. He can say that there are no less than b people standing in front of him and no more than apeople standing behind him. Find the number of different positions Vasya can occupy.

    Input

    As an input you have 3 numbers:

    1. Total amount of people in the line;

    2. Number of people standing in front of him

    3. Number of people standing behind him

    Examples

    Line.WhereIsHe(3, 1, 1) // => 2 The possible positions are: 2 and 3
    Line.WhereIsHe(5, 2, 3) // => 3 The possible positions are: 3, 4 and 5

    The third parameter is not irrelavant and is the reason why (9,4,3) is 4 not 5
    you have to satisfy both conditions
    no less than bef people in front of him
    and
    no more than aft people behind him

    as far as i can tell all the test cases are correct

    9个人,前面的人不少于4个,后面的人不多于3个的话,可以占据6,7,8,9 是个位置

    第五个位置,虽然前面是4个人,但是后面也是4个人。后面的人数超过3了,就不符合。

    using System;
    
    public class Line
        {
            public static int WhereIsHe(int p, int bef ,int aft)
            { 
               // Your code is here...
               int count=0;
               int a=0;//people infront of him
               int b=0;//people behind him
              for(int i=1;i<=p;i++)
              {
               a=i-1;
               b=p-i;
               if(a>=bef&&b<=aft)
               {
               count++;
               }
              }
              return count;
            }
        }

     使用Linq进行简化后:

    using System;
    using System.Linq;
    public static int WhereIsHe(int p, int bef, int aft)
    {
        return Enumerable.Range(1, p).Where(x => x - 1 >= bef && p - x <= aft).Count();
    }

    其他人的解法

    using System;
    
    public class Line
        {
            public static int WhereIsHe(int p, int bef ,int aft)
            { 
               return Math.Min(p-bef,aft+1);
            }
        }
  • 相关阅读:
    sqlite android
    cocos2dx 2.1.3 使用json
    cocos2dandroid 自动缩放、高清显示
    cocos2dx HttpClient
    coco2dx 2.1.3 之 使用网络请求
    facebook on android
    项目运行出错怎么办?
    Bug应对策略 本文系转
    从网页中导入Excel
    ASP.NET HTTP运行时组成详解[转帖]
  • 原文地址:https://www.cnblogs.com/chucklu/p/4621581.html
Copyright © 2011-2022 走看看