백준/백준-C++

1011번: Fly me to the alpha centauri

Beabletoet 2017. 1. 27. 14:09

#include <iostream>

using namespace std;

int main()

{

int T, x, y, m, count = 0, plus=1;

bool two = 0;

cin >> T;

for (int i = 0; i < T; ++i)

{

cin >> x >> y;

m = y - x;

for (int j = 0; j < m;++count)

{

if (two == 0)

{

j += plus;

two = 1;

}

else

{

j += plus;

++plus;

two = 0;

}

}

cout << count << endl;

count = 0; plus = 1; two = 0;

}

}


=====시간 초과..



#include <iostream>

using namespace std;

int main()

{

int T, spd=1, count=0;

long long x, y, m;

cin >> T;

for (int i = 0; i < T; ++i)

{

cin >> x >> y;

m = y - x;

while (m > 0)

if (m >= (spd * 2))

{

count += 2;

m -= (spd * 2);

++spd;

}

else

{

++count;

m -= spd;

--spd;

}

cout << count << endl;

spd = 1; count = 0;

}

}