A - Floor Number----清棂露

博客围绕ACM竞赛中的一道C语言算法题展开,题目是根据给定的房间号和每层房间数,计算所在楼层。介绍了输入输出格式及示例,给出解题思路,即先对房间号处理,再根据余数情况计算楼层,还提及有多种做法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

** 简单A - Floor Number清棂露**

Vasya goes to visit his classmate Petya. Vasya knows that Petya’s
apartment number is nn. There is only one entrance in Petya’s house
and the distribution of apartments is the following: the first floor
contains 22 apartments, every other floor contains xx apartments each.
Apartments are numbered starting from one, from the first floor. I.e.
apartments on the first floor have numbers 11 and 22, apartments on
the second floor have numbers from 33 to (x+2)(x+2), apartments on the
third floor have numbers from (x+3)(x+3) to (2⋅x+2)(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 nn apartments.
You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤10001≤t≤1000)
— the number of test cases. Then tt test cases follow. The only line
of the test case contains two integers nn and xx
(1≤n,x≤10001≤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
7 3
1 5
22 5
987 13
Output
3
1
5
77

题意:
本题意思就是找到她住在几层楼,n给的是她所住楼层号,x是除了第一层两个房间,每层的房间数,比如第一个案例(7 3)意思就是她房间号是7,除了第一层楼2个房间,其它每层有3个房间,便可以推出她在三楼,输出3,案例(1 5)只要n=1或n=2,不论x等于谁,都是在一楼,案例(22 5)也可以推出,数字小可以在纸上画画
思路:
因为第一层是固定的,所以n-2,(n-2)/x得商,然后(n-2)%x求余,判断是否有余数,如果有余数,则输出结果(即所求得商)加2,没有余数则输出结果加1
代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int t,n,x,l;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&x);
        if(n==1||n==2)
            printf("1\n");

    if(n>2)
    {
        l=(n-2)/x;
        if((n-2)%x==0)
        {
            l=l+1;
        }
        else if((n-2)%x!=0)
        {
            l=l+2;
        }
        printf("%d\n",l);
    }
    }
    return 0;
}

相信大家看代码可以理解,还有很多种做法,打卡第一天2020.11.19

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值