Floor Number

    科技2022-08-30  115

    Floor Number

    Vasya goes to visit his classmate Petya. Vasya knows that Petya’s apartment number is n.

    There is only one entrance in Petya’s house and the distribution of apartments is the following: the first floor contains 2 apartments,every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x+2), apartments on the third floor have numbers from (x+3) to (2⋅x+2), and so on. Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments. You have to answer t independent test cases.

    Input

    The first line of the input contains one integer t (1≤t≤1000) — the number of test cases. Then t test cases follow. The only line of the test case contains two integers n and x (1≤n,x≤1000) — the number of Petya’s apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).

    Output

    For each test case, print the answer: the number of floor on which Petya lives.

    Example

    Input

    4 3 1 5 22 5 987 13

    Output

    3 1 5 77

    Note

    Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.

    In the second test case of the example, Petya lives in the apartment 1 ``which is on the first floor.

    参考代码如下:

    #include <stdio.h> #include <math.h> int main() { int t,n,x; scanf("%d",&t); while(t--) { scanf("%d %d",&n,&x); if(n<=2) { printf("1\n"); } else { int f=0; if((n-2)%x==0) { f=(n-2)/x+1; } else { f=(n-2)/x+2; } printf("%d\n",f); } } return 0; }

    题意

    给定 n(要查询的公寓号)和 x(每层的公寓数),给出公寓号x,找出其所在的楼层数f。

    思路

    题中已经指出第一层只有两个公寓,所以要分类讨论x<=2和x>2。 分析x>2时,找规律!找规律!找规律!: 当(n-2)%x==0时,f=(n-2)/x+1; 当(n-2)%x !=0时,f=(n-2)/x+2; (n-2)%x代表(题目所给的公寓号 - 第一层固定的两个公寓)%(题目给出的每层的公寓数)

    Processed: 0.025, SQL: 10