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=;
int a=;//people infront of him
int b=;//people behind him
for(int i=;i<=p;i++)
{
a=i-;
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(, p).Where(x => x - >= 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+);
}
}
Where is Vasya?的更多相关文章
- Milliard Vasya's Function-Ural1353动态规划
Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning mathematician. He decided to make ...
- CF460 A. Vasya and Socks
A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 递推DP URAL 1353 Milliard Vasya's Function
题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...
- Codeforces Round #281 (Div. 2) D. Vasya and Chess 水
D. Vasya and Chess time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分
C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- codeforces 676C C. Vasya and String(二分)
题目链接: C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces Round #324 (Div. 2) C. Marina and Vasya 贪心
C. Marina and Vasya Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/pr ...
- Codeforces Round #322 (Div. 2) A. Vasya the Hipster 水题
A. Vasya the Hipster Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/p ...
- Codeforces Codeforces Round #319 (Div. 2) C. Vasya and Petya's Game 数学
C. Vasya and Petya's Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
随机推荐
- JQ批量控制form禁用
<script type="text/javascript" src="http://www.joy-city.com.cn/templets/default/sc ...
- zencart后台增加菜单选项
如果要在程序中使用额外的参数,在后台控制,添加到菜单属性 在后台 SQL脚本 运行如下 SQL语句 INSERT INTO configuration (configuration_title, co ...
- 四个基数任意次数组合相加得到一个数N,求所有可能组合
#include <iostream> #include <vector> usingnamespace std; vector<int> vec; constin ...
- 转 在无法通过yum下载非标准包时,怎么办
在CentOS下,我们可以通过yum来下载或更新rpm包,但是标准的源(repository)里只提供一部分的rpm包,虽然大部分情况下,这些包是够用的.但是有时候还是需要下载其他的一些非标准的包,如 ...
- 电网SVG简介
目 录1. 范围 12. 规范性引用文件 13. 缩略语 14. 本标准涉及的图形交换特征 ...
- sqlplus中"-S"和"-L"用法
Usage: SQLPLUS [option] [logon] [start] <option> ::= -H | -V | [ [-L] [-M ] [-R ] [-S] ] &qu ...
- MDI-多文档窗体
1.IsMdicontainer属性设置是否为多文档窗体 this.IsMdiContainer = true; 2.MdiParent属性设置为父窗体 Frm_Child frm = new Frm ...
- 【CSS】盒模型+选择器(你选择的要操作的对象)
盒模型 转http://www.cnblogs.com/cchyao/archive/2010/07/12/1775846.html 1.w3c标准的盒模型和ie的盒模型主要差别在于content的w ...
- springmvc整合redis架构搭建实例
新换环境,又有新东西可以学习了,哈皮! 抽空学习之余看了一下redis,个人对Springmvc的爱是忠贞不渝,所以整理了一下Springmvc整合redis的环境搭建.分享学习. 第一步: 创建ma ...
- sb 讲解 (!(~+[])+{})[--[~+""][+[]]*[~+[]] + ~~!+[]]+({}+[])[[~!+[]]*~+[]]
代码:(!(~+[])+{})[--[~+""][+[]]*[~+[]] + ~~!+[]]+({}+[])[[~!+[]]*~+[]] 输出sb. 分段解析: 首先解析s: (! ...