2009年2月17日 星期二

Q846: Steps

// Run time: 0.010
#include <stdio.h>
#include <math.h>
int calculate(int x, int y);
int main()
{
int n;
int step;
int a, b;
scanf("%d", &n);
while (n--)
{
scanf("%d%d", &a, &b);
step = calculate(a, b);
printf("%d\n", step);
}
return 0;
}

int calculate(int x, int y)
{
int sum = 0;
int weight = 2;
int n, cnt;

n = y-x;

if (!n)
return 0;

for ( ;sum < n; sum += weight, weight += 2);

x = (int)sqrt(sum);

if (n > x*x)
cnt = 2*x;
else
cnt = 2*x-1;

return cnt;
}

沒有留言: