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);
        }
    }
时间: 2024-08-05 19:36:58

Where is Vasya?的相关文章

Codeforces Round #281 (Div. 2) A. Vasya and Football 暴力

A. Vasya and Football Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red

codeforces 584C Marina and Vasya

C. Marina and Vasya Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string. More formally, you are given two strings s1, s2 of length

Codeforces Round #281 (Div. 2) C. Vasya and Basketball 排序

C. Vasya and Basketball Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from does

codeforces 493C Vasya and Basketball (枚举+模拟+思维)

C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 point

Ural 1353 Milliard Vasya&#39;s Function(DP)

题目地址:Ural 1353 定义dp[i][j],表示当前位数为i位时,各位数和为j的个数. 对于第i位数来说,总可以看成在前i-1位后面加上一个0~9,所以状态转移方程就很容易出来了: dp[i][j]=dp[i][j]+dp[i][j-1]+dp[i][j-2]+.......+dp[i][j-9]: 最后统计即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <

【codewar-6kyu】Vasya - Clerk

##题目描述 The new "Avengers" movie has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single100, 50 or 25 dollars bill. A "Avengers" ticket costs 25 dollars. Vasya is cur

Codeforces Round #281 (Div. 2) B. Vasya and Wrestling 水题

B. Vasya and Wrestling Vasya has become interested in wrestling. In wrestling wrestlers use techniques for which they are awarded points by judges. The wrestler who gets the most points wins. When the numbers of points of both wrestlers are equal, th

Codeforces Round #281 (Div. 2) D. Vasya and Chess 博弈

D. Vasya and Chess Vasya decided to learn to play chess. Classic chess doesn't seem interesting to him, so he plays his own sort of chess. The queen is the piece that captures all squares on its vertical, horizontal and diagonal lines. If the cell is

Codeforces Round #324 (Div. 2)C. Marina and Vasya set

                                                      C. Marina and Vasya Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string. Mor

CF 577C Vasya and Petya&#39;s Game

题意:一个游戏,A童鞋在1~n的范围里猜一个数,B童鞋询问一个集合,A童鞋要对集合里每个数做出回答,他猜的数能否给整除,B要通过这些答案得到A猜的数,最少需要猜哪些数? 解法:一个数可以由若干个质数的指数次幂相乘得到,所以只要询问小于n的所有质数的指数次幂就可以得到全部数的答案. 代码: #include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<