由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 一道program challenge的题
相关主题
问一道算法题求教一下,我的这个代码有什么问题
我花了一个小时才调通过这个程序诡异的number of islands.
求问一道动态规划的题目 (转载)问一个面试问题
一道Facebook题如何避免溢出请教一个DP的问题
问一个编程题这个题咋做?
菜鸟问个two sum的变型题Another amazon interview questions
面试题,min steps to reduce n to 1问几道面试题
求助:2倍年龄问题的通项解析式问题请教软件开发 的 几个面试题!
相关话题的讨论汇总
话题: input话题: peak话题: line话题: integer话题: sample
进入JobHunting版参与讨论
1 (共1页)
o******y
发帖数: 13
1
consider the process of stepping from integer x to integer y along the
integer points of the straight line. The length of each step must be non-
negative and can be one bigger than, equal to, or one smaller than the
length of the previous step.
What is the minimum number of steps in order to get from x to y? the length
of both the first and the last step must be 1.
INPUT:
the input begins with a line containing the number of test cases - n. Each
test case that follows consists of a line with two integers: 0 <= x <= y <=
2^31.
OUTPUT:
for each test case, print a line giving the minimal number of steps to get
from x to y.
Sample input:
3
45 48
45 49
45 50
Sample output:
3
3
4
很有趣的一道题:)
i**********e
发帖数: 1145
2
My solution:
int min_steps(int x, int y) {
int n = y-x;
if (n == 0) return 0;
int peak = (int)sqrt((double)n);
if (peak*peak == n)
return 2*peak-1;
else if (peak*(peak+1) >= n)
return 2*peak;
else
return 2*peak+1;
}
Update: 忘了检查 x==y 的特殊情况。。。加了特殊情况检测~
一些常见面试题的答案与总结 -
http://www.ihas1337code.com
1 (共1页)
进入JobHunting版参与讨论
相关主题
请教软件开发 的 几个面试题!问一个编程题
再上到题菜鸟问个two sum的变型题
请教一道面试题面试题,min steps to reduce n to 1
向大牛问一道电面题求助:2倍年龄问题的通项解析式问题
问一道算法题求教一下,我的这个代码有什么问题
我花了一个小时才调通过这个程序诡异的number of islands.
求问一道动态规划的题目 (转载)问一个面试问题
一道Facebook题如何避免溢出请教一个DP的问题
相关话题的讨论汇总
话题: input话题: peak话题: line话题: integer话题: sample