uva 846 - Steps
2024-09-14 03:58:08
找出步數與距離的關係即可得解。
- 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 ;
}
最新文章
- Centos 7.0 安装Mono 3.4 和 Jexus 5.6
- dell r710 安装ubuntu 12.04 server 启动后进入initramfs解决办法
- log4j日志文件配置说明及使用
- TestNG中用Parameters或DataProvider为测试方法传入参数
- 【原】训练自己haar-like特征分类器并识别物体(1)
- [cb]ScriptableObject 序列化
- Codeforces Round #331 (Div. 2) C. Wilbur and Points
- C语言计算程序运行时间
- PHP设置COOKIE的HttpOnly属性
- 使用Express创建一个简单的示例
- android:TextAppearance.Material.Widget.Button.Inverse问题
- 12.06 JavaScript
- jvm监控命令介绍
- c++堆栈实现
- Highest Rated Features
- (转载)Linux查看文件编码格式及文件编码转换
- Winform DevExpress控件库(三) 使用NavBarControl控件定制导航栏
- 2018-2019-2 网络对抗技术 20165328 Exp2 后门原理与实践
- QT 信号槽connect中解决自定义数据类型或数组作为函数参数的问题——QT qRegisterMetaType 注册MetaType——关键:注册自定义数据类型或QMap等容器类
- Beta 冲刺 (6/7)
热门文章
- Python交互模式下方向键出现乱码
- 03 - 替换vtkDataObject中的GetProducerPort()方法 VTK 6.0 迁移
- 重构遗留程序的一次案例学习(java程序)
- 将Altium中的原理图与PCB导出为PDF的步骤与方法
- USB 0xC0000012 错误详解
- mono环境变量
- GitHub的使用详解!Windows GitHub ,Sublime Git GitGutter的使用!
- BZOJ1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
- delphi编写dll心得, 谢谢原作者的分享。转
- ESB 企业服务总线