UVa 846 Steps (数学)
来源:互联网 发布:js 展示大图 编辑:程序博客网 时间:2024/05/02 19:55
846 - Steps
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=787
One steps through integer points of the straight line. The length of a step must be nonnegative and can be by one bigger than, equal to, or by one smaller than the length of the previous step.
What is the minimum number of steps in order to get from x to y? The length of the first and the last step must be 1.
Input and Output
Input consists of a line containing n, the number of test cases. For each test case, a line follows with two integers: 0xy < 231. For each test case, print a line giving the minimum number of steps to get from xto y.Sample Input
345 4845 4945 50
Sample Output
334
自己算9~16的步数,答案就看出来了。
完整代码:
/*0.015s*/#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 == 0)puts("0");else{n = (int)sqrt(diff);diff -= n * n;if (diff == 0)printf("%d\n", (n << 1) - 1);else if (diff <= n)printf("%d\n", n << 1);elseprintf("%d\n", (n << 1) + 1);}}return 0;}
- UVa 846 Steps (数学)
- UVA 846 - Steps(数学)
- uva 846 Steps(数学规律)
- UVA 846 (暑假-数学 -G - Steps)
- UVA 864-Steps 数学规律
- uva 846 Steps
- uva-846 steps
- uva 846 Steps
- UVA 846 - Steps
- uva 846 - Steps
- UVa 846 - Steps
- UVa 846 - Steps
- UVa 846-Steps
- UVa 846 - Steps
- UVA 846 Steps
- uva 846 - Steps
- uva 846 Steps
- UVa 846 Steps
- 2013年9月14日美团面试 被鄙视
- 开始新的征程
- java的system.arraycopy()方法
- javascript函数的基础功能
- Rman还原种子数据库练习
- UVa 846 Steps (数学)
- iOS中关于json解析的几种方式
- hibernate详解
- 【摘】初学C++异常处理 boost::bad_lexical_cast
- 汉诺塔(一) nyist88
- 九度OJ 题目1534:数组中第K小的数字(二分解)
- java基础--instanceof用法
- 关于ant
- ArcGIS Engine 如何重载DoubleClick事件