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 ...
随机推荐
- CentOS6.4 GOOGLE chrome install
yum install http://people.centos.org/hughesjr/chromium/6/i386/RPMS/chromium-28.0.1500.45-205727.i686 ...
- CSS jQuery 图片全屏切换
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- ural 1104 Don’t Ask Woman about Her Age
http://acm.timus.ru/problem.aspx?space=1&num=1104 #include <cstdio> #include <cstring&g ...
- C51的编程规范
现在单片机的程序设计,C51已经得到广泛的推广和应用,算是单片机的主流设计程序,甚至可以说作为单片机开发人员必须要掌握的一门语言了.作为一门工具,最终的目的就是实现功能.在满足这个前提条件下,我们希望 ...
- 进化计算简介和遗传算法的实现--AForge.NET框架的使用(六)
原文:进化计算简介和遗传算法的实现--AForge.NET框架的使用(六) 开学了,各种忙起来了… 上一篇介绍了AForge.NET在人工神经网络上的一点点使用,但是老觉不过瘾.matlab用着实在不 ...
- http状态码有那些?分别代表是什么意思
http状态码有那些?分别代表是什么意思? 简单版 [ 100 Continue 继续,一般在发送post请求时,已发送了http header之后服务端将返回此信息,表示确认,之后发送具体参数信息 ...
- COJ 0248 HDNOIP201408生成树
HDNOIP201408生成树 难度级别: A: 编程语言:不限:运行时间限制:5000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 输入 第一行包括两个整数V, ...
- 【KMP】Number Sequence
KMP算法 KMP的基处题目,数字数组的KMP算法应用. 主要是next[]数组的构造,next[]存储的是字符的当前字串,与子串前字符匹配的字符数. 移动位数 = 已匹配的字符数 - 对应的部分匹配 ...
- 从epoll构建muduo-11 单线程Reactor网络模型成型
mini-muduo版本传送门 version 0.00 从epoll构建muduo-1 mini-muduo介绍 version 0.01 从epoll构建muduo-2 最简单的epoll ver ...
- JavaScript 自动分页插件 datatables
DataTables Table plug-in for jQuery https://www.datatables.net/