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

  • 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 ;
}

uva 846 - Steps的更多相关文章

  1. Steps UVA 846

    说说:此题要求求出从整数x到达整数y所要经过的最短步数,且第一步和最后一步必须为一,同一时候每一步都比前一步多一步,少一步或一样.如果想搞清楚每一步详细是如何走的,那么这道题是相当麻烦的.考虑到前后两 ...

  2. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  3. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  4. ACM训练计划step 1 [非原创]

    (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...

  5. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  6. Volume 1. Maths - Misc

    113 - Power of Cryptography import java.math.BigInteger; import java.util.Scanner; public class Main ...

  7. 【转】UVa Problem 100 The 3n+1 problem (3n+1 问题)——(离线计算)

    // The 3n+1 problem (3n+1 问题) // PC/UVa IDs: 110101/100, Popularity: A, Success rate: low Level: 1 / ...

  8. PC/UVa 题号: 110106/10033 Interpreter (解释器)题解 c语言版

    , '\n'); #include<cstdio> #include<iostream> #include<string> #include<algorith ...

  9. uva 116 Unidirectional TSP (DP)

    uva 116 Unidirectional TSP Background Problems that require minimum paths through some domain appear ...

随机推荐

  1. 浮点数比较问题(float x 与 '零值'比较)

    今天在牛客网上看到一道面试题,看完之后着实吃了一惊,自己平常都没有在意,看似简单的问题,实则考验了语言的基本功. 据说这是腾讯的面试题: float x 与“零值”比较的if语句为? if (x == ...

  2. 利用FbinstTool+大白菜u盘工具,制作多系统启动U盘【转】

    一般制作多系统启动盘的教程都会要用到rub4dos+grubinst+ultraiso+msgdiyerl等等工具,一大串的工具列表让人望而生畏.其实大白菜里已经对这些工具做了非常好的封装,利用大白菜 ...

  3. css、js的相互阻塞

    先决条件:脚本前面存在外部样式 以下试验虽然是在chrome下,但是对于IE8+以及其他浏览器也适用. 1.内联脚本(http://jsbin.com/mudab/1) <!DOCTYPE ht ...

  4. web api 2 学习笔记 (OData Batch request)

    之前介绍过OData 中实现RPC的写法,今天在来一个批量操作. 参考 : https://damienbod.wordpress.com/2014/08/14/web-api-odata-v4-ba ...

  5. 如何关闭Altium Designer联网功能(图文教程)

    画PCB电路板的人们都知道Altium Designer这款软件,本文介绍如何避免收到Attorney,关掉Altium Designer 的自动联网功能,需要两步,介绍如下 打开AD软件, DXP- ...

  6. GitHub 如何基於 Node.js 和 Chromium 開發 Atom?

    看到回答里, 多数都没有回答到点子上, 还有些给了非常主观的意见而没有给出实际结论和分析过程. 题主的问题有四个: 1. Github 如何基于 Node.js 和 Chromium 开发 Atom? ...

  7. PHP上传文件出现文件名被截掉第一个字符的问题

    最近用PHP做了一个简单的上传功能,结果出现了一个意想不到的问题.我上传的文件,在获取$_FILES的时候发现文件名的第一个字符被截掉了,因为最开始上传的一直是数字或者字母为名称的文档,也没有太在意这 ...

  8. 美国易安信公司 EMC

    EMC 提供了帮助您利用这一资产的工具.凭着我们的系统.软件.服务和解决方案,我们能够与您一道为您的公司打造一个综合性的信息基础架构.我们帮助客户设计.构建和管理智能.灵活而且安全的信息基础架构.这些 ...

  9. [饭后算法系列] 数组中"和非负"的最长子数组

    1. 问题 给定一列数字数组 a[n], 求这个数组中最长的 "和>=0" 的子数组. (注: "子数组"表示下标必须是连续的. 另一个概念"子 ...

  10. openStack 手动部署文档

    1, 规划配置网络