Where is Vasya?

Source: Internet
Author: User

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 is no less than b people standing in front of him and people standing behind him no more than a . 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 are not irrelavant and are the reason why (9,4,3) are 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 is correct

9 people, not less than 4 people in front, not more than 3 people behind, can occupy 6,7,8,9 is a position

The fifth position, although the front is 4 people, but the back is also 4 people. After the number of people over 3, it does not fit.

usingSystem; Public classLine { Public Static intWhereishe (intPintBEF,intaft) {            //Your code is here ...           intCount=0; intA=0;//people infront of him           intb=0;//people behind him           for(intI=1; i<=p;i++) {a=i-1; b=p-i; if(a>=bef&&b<=aft) {Count++; }          }          returncount; }    }

After using LINQ for simplification:

using System; using System.Linq;  Public Static int Whereishe (intintint , aft)    {return enumerable.range (1  1 >= bef && p-x <= aft). Count ();}

Other people's Solution

using System;  Public class Line    {        publicstaticint whereishe (intint bef,int aft)        {            return math.min (p-bef,aft+1);        }    }

Where is Vasya?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.