找出步數與距離的關係即可得解。

  • 0步最多能抵達的距離是0
  • 1步最多能抵達的距離是1(1)
  • 2步最多能抵達的距離是2(1 1)
  • 3步最多能抵達的距離是4(1 2 1)
  • 4步最多能抵達的距離是6(1 2 2 1)
  • 5步最多能抵達的距離是9(1 2 3 2 1)
  • 6步最多能抵達的距離是12(1 2 3 3 2 1)
  • ……以此類推
 #include <iostream>
#include <cstdio>
#include <cmath>
#define ERROR 1e-10
using namespace std;
int main(){
int n, x, y;
int dis, step;
while( scanf( "%d", &n ) != EOF ){
for( int i = ; i < n ; i++ ){
scanf( "%d%d", &x, &y ); dis = y-x;
if( dis == ){
printf( "0\n" );
continue;
} step = (int)(sqrt((double)dis)+ERROR);
if( step * step == dis ) step = step * - ;
else if( step * step + step < dis ) step = step * + ;
else step = step * ; printf( "%d\n", step );
}
}
return ;
}

另:

 #include <iostream>
using namespace std; int main()
{
int x, y;
int testCases;
int min_steps = ;
cin >> testCases;
while(testCases --)
{
cin >> x >> y;
int difference = y - x;
min_steps = ; if(difference != )
{
int sumOfSteps = ;
int z = ; //divided by 2, it represents the size if the next step while(difference > sumOfSteps)
{
sumOfSteps += (z / ); // next step
min_steps ++;
z++;
}
}
cout << min_steps << endl;
}
return ;
}
 #include<cstdio>
#include<cmath> int main(void)
{
int t, x, y, diff, n;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &x, &y);
diff = y - x;
if (diff == )
puts("");
else
{
n = (int)sqrt(diff);
diff -= n * n;
if (diff == )
printf("%d\n", (n << ) - );
else if (diff <= n)
printf("%d\n", n << );
else
printf("%d\n", (n << ) + );
}
}
return ;
}

最新文章

  1. Centos 7.0 安装Mono 3.4 和 Jexus 5.6
  2. dell r710 安装ubuntu 12.04 server 启动后进入initramfs解决办法
  3. log4j日志文件配置说明及使用
  4. TestNG中用Parameters或DataProvider为测试方法传入参数
  5. 【原】训练自己haar-like特征分类器并识别物体(1)
  6. [cb]ScriptableObject 序列化
  7. Codeforces Round #331 (Div. 2) C. Wilbur and Points
  8. C语言计算程序运行时间
  9. PHP设置COOKIE的HttpOnly属性
  10. 使用Express创建一个简单的示例
  11. android:TextAppearance.Material.Widget.Button.Inverse问题
  12. 12.06 JavaScript
  13. jvm监控命令介绍
  14. c++堆栈实现
  15. Highest Rated Features
  16. (转载)Linux查看文件编码格式及文件编码转换
  17. Winform DevExpress控件库(三) 使用NavBarControl控件定制导航栏
  18. 2018-2019-2 网络对抗技术 20165328 Exp2 后门原理与实践
  19. QT 信号槽connect中解决自定义数据类型或数组作为函数参数的问题——QT qRegisterMetaType 注册MetaType——关键:注册自定义数据类型或QMap等容器类
  20. Beta 冲刺 (6/7)

热门文章

  1. Python交互模式下方向键出现乱码
  2. 03 - 替换vtkDataObject中的GetProducerPort()方法 VTK 6.0 迁移
  3. 重构遗留程序的一次案例学习(java程序)
  4. 将Altium中的原理图与PCB导出为PDF的步骤与方法
  5. USB 0xC0000012 错误详解
  6. mono环境变量
  7. GitHub的使用详解!Windows GitHub ,Sublime Git GitGutter的使用!
  8. BZOJ1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
  9. delphi编写dll心得, 谢谢原作者的分享。转
  10. ESB 企业服务总线