Floor Number
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代表(题目所给的公寓号 - 第一层固定的两个公寓)%(题目给出的每层的公寓数)
Floor Number相关推荐
- 随心所欲的“四舍五入” 之 FLOOR函数如何使用?
Excel的FLOOR函数主要作用向下舍入为指定的倍数 本文介绍Microsoft Excel中FLOOR函数的公式语法和用法. FLOOR函数功能 FLOOR 函数用于向下舍入为指定的倍数,计算保留 ...
- 中英翻译《The number thirteen数字13》
The number thirteen 数字13 一.Pre-reading activity 阅前思考 1.Do you have a lucky number? If so, what is it ...
- Number - 十进制和二进制互相转换
一 目录 不折腾的前端,和咸鱼有什么区别 目录 一 目录 二 前言 三 字符串求和/求差 四 十进制转二进制 五 二进制转十进制 六 总结 二 前言 在刷题的生涯中,你会碰到让你尴尬的一个问题: 十进 ...
- excel函数实战(CEILING函数,ROUND函数,FLOOR函数的语法)
CEILING函数:CEILING函数用于将数字向上取整,即向远离零的方向取整. 语法:CEILING(number, significance) 其中,number是要向上取整的数,signific ...
- 向下取整floor函数
floor函数 floor(x),有时候也写做Floor(x),其功能是"下取整",或者说"向下舍入",即取不大于x的最大整数(与"四舍五入" ...
- 向下取整floor 和向上取整函数ceil
floor(x),有时候也写做Floor(x),其功能是"向下取整",或者说"向下舍入",即取不大于x的最大整数(与"四舍五入"不同,下取整 ...
- floor函数 并利用floor函数实现四舍五入功能
floor函数:其功能是"下取整",或者说"向下舍入",即取不大于x的最大整数(与"四舍五入"不同,下取整是直接去掉小数部分) 在#incl ...
- matlab中floor函数,floor函数_怎么在excel中使用floor函数
floor函数即上取整函数,是计算机C语言中的数学函数,与ceil函数相对应.但是它在excel中却是另一种含义,FLOOR函数是向下舍入为最接近指数基数的倍数,下面小编就教你怎么在excel中使用f ...
- c语言中floor有什么作用,Excel里的FLOOR函数怎么用?FLOOR函数的作用是什么
FLOOR函数怎么用?FLOOR函数的作用是向下取整,按照用户设置的预定值来舍入整数的数值,和四舍五入有异曲同工之妙,在一些需要以基本线为标准的数据中向下取整的用户非常之大,具体使用方法请看下文. F ...
最新文章
- AndFix解析——(中)
- orcle抽数据到mysql_抽取oracle数据到mysql数据库的实现过程
- Python 中的模块和包
- C# 和 VB.NET 下,
- I - Washing clothes
- 动态通过网络获取json来tabbar图片和文字或其他信息
- eclipse html插件_欲善事先利器——IDEA 插件篇
- KinhDown_v2.4.42稳定版 百度云最新不限速下载工具
- css定位,的重新理解,仔细理解描述即可
- mysql 瓶颈诊断_MySQL 监控、性能瓶颈排查
- 输入一个整型数组,数组里有正数也有负数。数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。要求时间复杂度为O(n)
- Linux中如何使用帮助
- 单元测试 代码里面都绝对路径怎么处理_python基础之包,异常处理
- python进阶之内置方法
- 【Practical】ZFC七公理
- 崔希凡-javaWeb-笔记day07-day09(2016年7月26日23:17:27)
- 微信注册页面html实现,微信注册.html
- 【电脑新硬盘如何分区】
- 用 NetworkX + Gephi + Nebula Graph 分析<权力的游戏>人物关系(上篇)
- python excel画图