首页 > 其他分享 >UVa 846 Steps (数学)

UVa 846 Steps (数学)

时间:2023-04-12 13:07:46浏览次数:51  
标签:846 length steps step Steps Input UVa line diff


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 

x

to 

y

.

Sample Input

3
45 48
45 49
45 50


Sample Output


3
3
4



自己算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);
			else
				printf("%d\n", (n << 1) + 1);
		}
	}
	return 0;
}



标签:846,length,steps,step,Steps,Input,UVa,line,diff
From: https://blog.51cto.com/u_5535544/6185471

相关文章

  • UVa 253 Cube painting (模拟)
    253-CubepaintingTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=189Wehaveamachineforpaintingcubes.Itissuppliedwiththreedifferentcolors:bl......
  • UVa 11498 Division of Nlogonia (water ver.)
    11498-DivisionofNlogoniaTimelimit:1.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2493TheProblemAftercenturiesofhostilitiesandskirmishesbetweenthefour......
  • UVa 11723 Numbering Roads (water ver.)
    11723-NumberingRoadsTimelimit:1.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2823Inmycountry,streetsdon’thavenames,eachofthemarejustgivenanumber......
  • UVa 10719 Quotient Polynomial (数学)
    10719-QuotientPolynomialTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=1660Apolynomialofdegree n canbeexpressedasIf k isanyintegerthenwecan......
  • UVa 103 Stacking Boxes (DP&DAG)
    103-StackingBoxesTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=39BackgroundSomeconceptsinMathematicsandComputerSciencearesimpleinoneortw......
  • UVa 11129 An antiarithmetic permutation (构造题&想法题&分治)
    11129-AnantiarithmeticpermutationTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=2070Apermutationof n+1 isabijectivefunctionoftheinitial n+1......
  • UVa 10041 Vito's Family (中位数&快速选择)
    10041-Vito'sFamilyTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=982BackgroundTheworld-knowngangsterVitoDeadstoneismovingtoNewYork.He......
  • UVa 706 / POJ 1102 LCD Display (模拟)
    706-LCDDisplayTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=647http://poj.org/problem?id=1102Afriendofyouhasjustboughtanewcomputer.Untilno......
  • UVa 757 / POJ 1042 / East Central North America 1999 Gone Fishing (枚举&贪心&想
    757-GoneFishingTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=698http://poj.org/problem?id=1042Johnisgoingonafishingtrip.Hehas h hoursavailable( ),andther......
  • UVa 408 Uniform Generator (最大公约数&证明)
    408-UniformGeneratorTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=349Computersimulationsoftenrequirerandomnumbers.Onewaytogeneratepseudo-r......