现你准备搬入这条街,你能搬入一幢房子的条件是这幢房子没有人住在里面,与此同时由于你非常热爱与邻居进行交流,故而你需要你所入住的房子两边上都有住户。
现要你求最小的可能符合要求的房子数,以及最大的可能符合要求的房子数。
最大的情况为(#-#-#-),此种情况有二个位置满足条件,为最大,故输出2
输入的一行为测试用例数t(1 <= t <= 200000),
接下来t行,每行含两个整数n和k,(1 <= n <= 1,000,000,000,0 <= k <= n)
对于每个用例输出最小的可能数以及最大的可能数
6 1 0 1 1 2 0 2 1 2 2 6 4
0 0 0 0 0 0 0 0 0 0 0 2
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int k = sc.nextInt(); if(k<2||k==n||n<3){ System.out.println(0+" "+0); }else if(n-k<k){ System.out.println(0+" "+(n-k)); }else{ System.out.println(0+" "+(k-1)); } } } }
我的
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int a=sc.nextInt(); long min=0,max; for(int i=1;i<=a;i++){ long n=sc.nextLong(); long k=sc.nextLong(); if(n<3||k<2){ max=0; }else{ if(n%2==0){//n为双数 if(k==n/2||k==n/2+1){ max=n/2-1; }else if(k<n/2){ max=k-1; }else{ max=n-k; } }else{//n为单数 if(k==n/2+1){ max=n/2; }else if(k<n/2+1){ max=k-1; }else{ max=n-k; } } } System.out.println(min+" "+max); } } }
别人的浓缩版
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int test = sc.nextInt(); for(int i=0; i<test; i++){ int n = sc.nextInt(); int k = sc.nextInt(); //住满了 if(n == k || n<3 || k<2){ System.out.println("0 0"); } else{ int result = (n/2 >= k)?(k-1):(n-k); System.out.println("0 "+result); } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); if (n < 3) { System.out.println(0 + " " + 0); } else { if (n - k < k) { System.out.println(0 + " " + (n - k)); } else { System.out.println(0 + " " + (k - 1 > 0 ? k - 1 : 0)); } } } } }