Codeforces 638A:Home Numbers(规律)
来源:互联网 发布:js中函数的定义 编辑:程序博客网 时间:2024/06/06 19:50
The main street of Berland is a straight line with n houses built along it (n is an even number). The houses are located at both sides of the street. The houses with odd numbers are at one side of the street and are numbered from 1 ton - 1 in the order from the beginning of the street to the end (in the picture: from left to right). The houses with even numbers are at the other side of the street and are numbered from2 to n in the order from the end of the street to its beginning (in the picture: from right to left). The corresponding houses with even and odd numbers are strictly opposite each other, that is, house 1 is opposite house n, house 3 is opposite house n - 2, house 5 is opposite house n - 4 and so on.
Vasya needs to get to house number a as quickly as possible. He starts driving from the beginning of the street and drives his car to housea. To get from the beginning of the street to houses number1 and n, he spends exactly1 second. He also spends exactly one second to drive the distance between two neighbouring houses. Vasya can park at any side of the road, so the distance between the beginning of the street at the houses that stand opposite one another should be considered the same.
Your task is: find the minimum time Vasya needs to reach house a.
The first line of the input contains two integers, n anda (1 ≤ a ≤ n ≤ 100 000) — the number of houses on the street and the number of the house that Vasya needs to reach, correspondingly. It is guaranteed that numbern is even.
Print a single integer — the minimum time Vasya needs to get from the beginning of the street to housea.
4 2
2
8 5
3
In the first sample there are only four houses on the street, two houses at each side. House2 will be the last at Vasya's right.
The second sample corresponds to picture with n = 8. House5 is the one before last at Vasya's left.
题目大意:房子的编号如图所示,车子从左往右出发,每经过一个对房子需要耗时一分钟,问你走到所需要的房子所用时间。
解题思路:观察图可发现,每一对相对的房子的编号总和为n+1,且到达奇数编号的房子所需要的时间为奇数房子的编号+1再除2,那么将编号为偶数的房子转化为对面的奇数房子的编号,再按奇数房子求即可。
代码如下:
#include <stdio.h>int main(){int n,a;scanf("%d%d",&n,&a);if(a%2==1){printf("%d\n",(a+1)/2);}else{int ji=n+1-a;printf("%d\n",(ji+1)/2);}return 0;}
- Codeforces 638A:Home Numbers(规律)
- 【CodeForces】638A - Home Numbers(水)
- CodeForces 638 A.Home Numbers(水~)
- CodeForces 638A Home Numbers
- 【CodeForces】638A - Home Numbers
- 【CodeForces】[638A]Home Numbers
- 【Codeforces】-638A-Home Numbers
- CodeForces 638 A Home Numbers(数学,水题)
- Codeforces 638A Home Numbers 【水题】
- CodeForces 638 A Home Numbers(水。)
- CodeForces - 630C Lucky Numbers (数学规律)
- 【CodeForces 397B 】On Corruption and Numbers(规律)
- codeforces 185A. Plant(规律)
- Codeforces--630E--A rectangle(规律)
- codeforces 727A 规律
- Codeforces 134A-Average Numbers(思维)
- codeforces 13A. Numbers
- CodeForces 13A - Numbers
- 认知神经科学--细胞机制与认知
- c++ 第七次上机实验
- vc++的编译选项
- 【增强学习】Torch中的增强学习层
- 我的模板库
- Codeforces 638A:Home Numbers(规律)
- RTC对移动开发IDE的支持
- java基础-filter
- cocos2dx的命令行编译出现 error: undefined reference to ‘xxx’
- C++第7次实验—最大公约数和最小公倍数
- 通过NSURLSessionConfiguration构建NSURLSessionDataTask
- C++第七次作业
- C++实验7—最大公约数和最小公倍数
- 逻辑运算和位运算