Where is Vasya?

寻找Vasya的位置
本文介绍了一个算法问题,即在已知队伍总人数及Vasya前方和后方人数限制的情况下,找出Vasya可能站的位置数量。文章提供了两种解决方案,并使用了C#语言进行实现。
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);
        }
    }

 

题目描述 Every fall, all movies are shown to a full house at one of the most popular cinema theatres in Yekaterinburg because students like to spend their time sitting in a cosy chair and watching a movie instead of attending lectures and seminars. Unfortunately, the distance between the rows of seats in the cinema hall is small, and people constantly stumble over other people's feet as they get to their seats before the film exhibition. Entering the hall, a visitor chooses from which end of the row (left or right) he will make his way to his seat. He chooses it in such a way that the number of people over whose feet he will stumble will be minimal. If these numbers are equal for the left and right ends, the visitor chooses the end of the row which is closer to his seat. Student of the Department of Philosophy Vasya is an enthusiastic movie-goer and an equally enthusiastic hater of mathematics. He was the first to buy a ticket to the first exhibition of a new movie. When Vasya entered the hall and sat down in his seat, he saw that other seats in the row were still unoccupied. Vasya knew that by the time the exhibition started the hall would be full. Therefore, quite a number of other visitors would stumble over his feet while getting to their seats. Despite his hatred for mathematics, Vasya was able to instantly estimate the maximal number of people that would stumble over his feet before the exhibition. Can you do the same? 每年秋天,叶卡捷琳堡最受欢迎的电影院之一的所有电影都会座无虚席,因为学生们喜欢坐在舒适的椅子上看电影,而不是参加讲座和研讨会。不幸的是,电影院里的两排座位之间的距离很小,人们在电影放映前就座时经常被别人绊倒。进入大厅后,访客可以选择从排的哪一端(左端或右端)就座。他选择的方式是让他绊倒的人数最少。如果左右两端的数字相等,则访客会选择最靠近他座位的那排末端。 哲学系学生瓦夏 (Vasya) 是一名狂热的电影爱好者,同时也是一名数学狂热爱好者。他是第一个购买新电影首映门票的人。当瓦夏走进大厅,在自己的座位上坐下时,他看到这一排的其他座位还空着。瓦夏知道,当展览开始时,大厅里已经挤满了人。因此,不少其他观众在入座时都会被他的脚绊倒。尽管瓦夏讨厌数学,但他还是能够立即估计出在展览前被他绊倒的最大人数。你能做同样的事吗? 输入 Input The only input line contains the total number of seats n in the row where Vasya is sitting and the number of his seat k (1 ≤ k ≤ n ≤ 50; n is even). These integers are separated with a space. The seats in the row are numbered starting with one. 唯一的输入行包含Vasya 所在行的 座位总数 n以及他的座位号k (1 ≤ k ≤ n ≤ 50; n 为偶数)。这些整数之间用空格分隔。一排的座位是从一开始编号的。 输出 Output Output the maximal number of people who would stumble over Vasya's feet. 输出最多有多少人会被 Vasya 绊倒。 样例输入 复制 4 1 样例输出 复制 1给出C++代码
09-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值