牛牛和妞妞正在玩一个猜数游戏,妞妞心里想两个不相等的正数,把这两个正数的和y告诉牛牛。
妞妞声称这两个数都不超过x,让牛牛猜这两个数是多少。
牛牛每猜一次,妞妞会告诉他猜对了还是猜错了,猜对了就停止游戏,猜错了就直到牛牛猜对为止。妞妞为了加大难度,有时会误报x的大小,如果牛牛可以判断出了这个x是错误的,就会直接询问妞妞答案。
牛牛最坏情况下要猜多少次才能猜到妞妞想的数呢?
两个整数x,y。1<=x,y<=1014。
一个数n,表示牛牛在最坏情况下猜测的次数。
7 10
2
4 10
0
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] params = br.readLine().trim().split(" "); long x = Long.parseLong(params[0]); long y = Long.parseLong(params[1]); if(2*x <= y){ // 两个数都不超过x,加起来是不可能为y的 System.out.println(0); }else{ if(x < y){ if(y % 2 == 0) System.out.println((2*x - y) / 2); else System.out.println((2*x - y) / 2 + 1); }else{ System.out.println(y / 2); } } } }
x,y = [int(x) for x in input().split()] left = max(1,y-x) right = min(x,y) times = right - left + 1 if y%2==0: times-=1 times = (times)//2 if times<0: times = 0 print(times)
#include <bits/stdc++.h> using namespace std; int main() { long long x,y; cin>>x>>y; if(x*2<y) { cout<<0<<endl; return 0; } long long high, middle, num = 0; if(x>=y) { high = y - 1; middle = y/2; num = high - middle; if(y%2==0) num = num + 1; } else { high = x; middle = y/2; num = high - middle; if(y%2==0) num = num + 1; } cout<<num<<endl; return 0; }
/* 假设两个数时a,b 想法:首先当2*x<y的时候,这个x就是错的,直接输出0 然后判断x与y的大小关系,当x<Y时,这时候a,b,就是根据x来变化的,a取值最大为x,b起始位y-x,递减,b递增 当y<x的时候,a,b就是依据y来决定,a最大为y,b起始位0 示例: 7 10 a b 7 3 6 4 5 5 后面是重复的 示例:8 5 a b 5 0 4 1 3 2 后面是重复的 这个规律我们发现只需要用(x-(y-x)+1)/2或者y/2就可以得到最坏的次数 */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main{ public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] str = br.readLine().split(" "); long x = Long.parseLong(str[0]); long y = Long.parseLong(str[1]); if(x*2<y){ System.out.println(0); return; } if(x<y) System.out.print((x-(y-x)+1)/2); if(y<=x) System.out.print(y/2); } }
<?php /* *n1+n2 = $y *n1|n2 <= $x *n1!=n2 */ function a($x, $y){ // 判断x是错误的 if ((2*$x-1) < $y) { return 0; } // 如果x > y 则重置x为y-1 if($x>$y){ $x = $y-1; } $a1 = $y/2; // 取中间值 $a2 = $y%2; // 取余 return $a2 ==0 ? $x-$a1: ($x-$a1)+0.5; } if(fscanf(STDIN, "%d %d", $a, $b) == 2){ echo a($a,$b); }
#include <iostream> using namespace std; int main() { long long x; long long y; long long a; long long b; cin >> x; cin >> y; //if x error if(y > 2*x || y < 2 || x < 1) { cout << 0 << endl; } else { //x 比 y大也是一种符合要求的情况,即不重复的所有结果 if(x >= y) x = y - 1; //所有结果数,去除a == b long long totoalReslut = (y - 1) / 2; //去除过大或过小的非结果成分 long long notResult = y - (x + 1); //结果 long long reslut = totoalReslut - notResult; cout << reslut << endl; } return 0; }
var line=readline().split(' ').map(Number); var max=line[0]; var sum=line[1]; // 表示较小的x if(sum>max){ var left=sum-max; }else{ // 给定的不超过的值太大了,和为5,每个数不超过8,这样的话就重设置max max=sum-1; var left=sum-max; } // 表示较大的x var right=max; // 此时就是说谎了 // 两个数不能相等 if(left>max||left==right||left>right){ console.log(0) }else{ var tem=Math.ceil((right-left)/2); console.log(tem) }
while(line=readline()){ line=line.split(" ") let x=parseInt(line[0]); let y=parseInt(line[1]); if(x>=y){ print(Math.floor((y-1)/2)); } else{ if(y>2*x){ print(0) } else{ let max=Math.max(x,(y-x)); let min=y-max print(Math.floor((max-min)/2)+1); } } }