CodeForces 638 A Home Numbers(数学,水题)

29 篇文章 0 订阅
18 篇文章 0 订阅
Description
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 to n - 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 from 2 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 house a. To get from the beginning of the street to houses number 1 and n, he spends exactly 1 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.

Input
The first line of the input contains two integers, n and a (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 number n is even.

Output
Print a single integer — the minimum time Vasya needs to get from the beginning of the street to house a.

Sample Input
Input
4 2
Output
2
Input
8 5
Output

3

代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
	int n,a;
	while(scanf("%d %d",&n,&a)!=EOF)
	{
		int q[50001],w[50001];
		memset(q,0,sizeof(q));
		memset(w,0,sizeof(w));
		for(int i=1;i<=n/2;i++)
		{
			q[i]=2*i-1;
			w[i]=n-2*i+2;
			if(q[i]==a||w[i]==a)
			{
				printf("%d\n",i);
				break;
			}
		}
	}
	return 0;
}
题意:一个人开车出去玩。路两边是房子,上边是单数1~n-1,下边是偶数n~2;告诉你路边总共有多少房子,还有车经过两个房子间所花费时间为一分钟,问多长时间这个人才能到达他的目的地。

思路:直接定义两个数组保存路两边房子的编号,for循环运算n/2/次,直接每次赋值时判断是不是目的地就行了。是就输出i不是继续运行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值