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

  • 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. 转:Twitter.com在用哪些Javascript框架?

    原文来自于:http://blog.jobbole.com/63964/ 我一直在研究twitter.com使用的一些UI框架.下面是这些框架的清单(大部分是Javascript框架).如果你发现有些 ...

  2. Mssql显错和不显错模式下的注入

    Title:Mssql显错和不显错模式下的注入 -- 2010-10-27 19:51 近期用手工注入用习惯了,便列出最近用的Mssql语句,以后方便拿来用! -------------------- ...

  3. Currency 货币 filter

    angularjs 其实也有一个currency指令,不过好像只是换符号而已. 这里自己写了一个简单的兑换率filter <div ng-controller="ctrl"& ...

  4. Java JDBC中,MySQL字段类型到JAVA类型的转换

    1. 概述 在使用Java JDBC时,你是否有过这样的疑问:MySQL里的数据类型到底该选择哪种Java类型与之对应?本篇将为你揭开这个答案. 2. 类型映射  java.sql.Types定义了常 ...

  5. CAD2014启动出现loadlibrary failed with error 87

    系统win8.1 x64 安装AutoCAD2014完成后,启动出现:Loadlibrary failed with error 87:参数错误 重启,重装都没用.查了一晚上,在国外网站上找到解决办法 ...

  6. P2P网贷投资须谨防圈钱人

    摘要:P2P领域依然呈现投资热潮,甚至部分国有大行也有意涉足.这个行业到底怎么了?P2P平台上高收益的理财产品,到底能买不?     新年才刚刚过了10天,就有4家网贷平台被爆出支付危机,P2P一时被 ...

  7. HDOJ 1163 Eddy's digital Roots(九余数定理的应用)

    Problem Description The digital root of a positive integer is found by summing the digits of the int ...

  8. codeforces257 div2 D最短路条数

    题意: 给一个无向图,总共有 n个点,m+k条边,给定点所连的k条边可以选择删除 问最多删除多少条可以保持该定点到其他点的最短路不变 题解: 从定点出发做单元最短路 首先如果定点到某个点的最短路小于 ...

  9. hdu-1598

    思路: 首先如果想到了Kruskal算法,那么下一步我们可能马上会想:那我们就从头开始写这个算法吧.然后一写就很容易发现一个问题——如果按照正常的Kruskal算法来做,那么start到end的舒适度 ...

  10. New Year Permutation(Floyd+并查集)

    Description User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to mak ...