B-simple game 周赛-B-水题-2020.5.18

B-simple game

One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let’s assume that Misha chose number m, and Andrew chose number a.

Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.

Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.

More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).

Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.

Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.

Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.

In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.

数字n,代表1~n这个范围,数字m,代表米莎选的数字。如果n为奇数,判断是不是m在数轴中点,如果是为m-1;
如果不是,若m在前半段,m+1;否则m-1.
注意:如果n为1,直接输出1.

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
typedef long long ll;

int main()
{
    int n, m;
    cin >> n >> m;
    if (n == 1) {
        cout << 1 << endl;
        return 0;
    }
    if (n % 2 == 1) {
        if (n / 2 + 1 == m) {
            printf("%d\n", m - 1);
            return 0;
        }
    }
    if (m <= n / 2) {
        printf("%d\n", m + 1);
        return 0;
    }
    else {
        printf("%d\n", m - 1);
        return 0;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值