uva 846 - Steps
找出步數與距離的關係即可得解。
- 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的更多相关文章
- Steps UVA 846
说说:此题要求求出从整数x到达整数y所要经过的最短步数,且第一步和最后一步必须为一,同一时候每一步都比前一步多一步,少一步或一样.如果想搞清楚每一步详细是如何走的,那么这道题是相当麻烦的.考虑到前后两 ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- ACM训练计划step 1 [非原创]
(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- Volume 1. Maths - Misc
113 - Power of Cryptography import java.math.BigInteger; import java.util.Scanner; public class Main ...
- 【转】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 / ...
- PC/UVa 题号: 110106/10033 Interpreter (解释器)题解 c语言版
, '\n'); #include<cstdio> #include<iostream> #include<string> #include<algorith ...
- uva 116 Unidirectional TSP (DP)
uva 116 Unidirectional TSP Background Problems that require minimum paths through some domain appear ...
随机推荐
- WebSQL实例记录
<table id="content"> </table> <br> <input type="button" id= ...
- PHP自定义弹出消息类,用于弹出提示信息并返回
一个用PHP自写的弹出消息类,用于在程序出错时弹出提示,,弹出警告框,或在程序运行到某阶段的快捷提示,需用时只需传入参数即可,函数并不复杂,但觉得挺实用.具体代码: function Alert($a ...
- 零基础创建RCP工程
一.环境搭建 1. 安装java jdk,我选择的是jdk 1.7版本,配置环境变量: 2. 下载并安装java EE: 二.创建工程 1. 打开File-> New ->other -& ...
- C程序设计语言练习题1-19
练习1-19 编写函数reverse(s),将字符串s中的字符顺序颠倒过来.使用该函数编写一个程序,每次颠倒一个输入行中的字符顺序.代码如下: #include <stdio.h> // ...
- winform中WebBrowser控件执行脚本
在实际应用中我们可能需要WebBrowser控件主动执行我们需要的脚本,执行脚本现在有两种方法. 1.WebBrowser控件加载脚本,简单方便,适用于简短的脚本,无法执行复杂操作. webBrows ...
- Solr Update备注
参考资料: http://wiki.apache.org/solr/ExtractingRequestHandler#Sending_documents_to_Solr /update 标准的upda ...
- java-Mysql-SQLServer数据类型匹配速查表
java-Mysql-SQLServer数据类型匹配速查表 Mysql ************************************ 当前列 ClassName ColumnType Di ...
- MCS-51单片机存储器结构
MCS-51的存储器可分为四类: 程序存储器 一个微处理器能够聪明地执行某种任务,除了它们强大的硬件外,还需要它们运行的软件,其实微处理器并不聪明,它们只是完全按照人们预先编写的程序而执行之.那么设 ...
- tigerVNC远程桌面,跨内网
tigerVNC的简单使用教程(CentOS的远程桌面连接) 1.环境和软件准备 (1) CentOS 6.5下 [root@localhost ~]$ yum install tigervnc (2 ...
- N次剩余
$$求解x^n\equiv a(\%P),其中P是质数,0\leq x<P$$ 设$g$是$P$的原根 那么$g^0,g^1,...,g^{P-2}$和$1,2,...,P-1$是一一对应的. ...