解题思路

  1. 题目本质是要找出在给定范围内,妹妹最多能错误报告多少次才能被牛牛识破
  2. 假设妹妹想的两个数之和为 ,那么:
    • 实际数字最大不能超过 (因为需要两个不相等的正整数)
    • 牛牛猜的数字不能超过
  3. 妹妹每次报告"猜错了"时,牛牛实际上已经猜对了
  4. 当牛牛猜到 时,一定能判断出妹妹在说谎(因为剩下的数不可能构成和为
  5. 因此,最多能错误报告的次数就是:

代码

#include <iostream>
using namespace std;

int main() {
    long long x, y;
    cin >> x >> y;
    
    long long n = y / 2 + 1;
    x = x > y - 1 ? y - 1 : x;
    long long ret = x - n;
    cout << (ret < 0 ? 0 : ret + 1) << endl;
    
    return 0;
}
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long x = sc.nextLong();
        long y = sc.nextLong();
        
        long n = y / 2 + 1;
        x = Math.min(x, y - 1);
        long ret = x - n;
        System.out.println(ret < 0 ? 0 : ret + 1);
    }
}
x, y = map(int, input().split())

n = y // 2 + 1
x = min(x, y - 1)
ret = x - n
print(0 if ret < 0 else ret + 1)

算法及复杂度

  • 算法:数学推导
  • 时间复杂度: - 只需要简单的数学计算
  • 空间复杂度: - 只使用常数额外空间