알고리즘 문제 풀이
[백준] - Fly me to the Alpha Centauri - 복습
pinok1o
2021. 1. 31. 12:00
https://www.acmicpc.net/problem/1011
제곱수 몇개를 생각해보면
4 = 1 + 2 + 1
9 = 1 + 2 + 3 + 2 + 1
16 = 1 + 2 + 3 + 4 + 3 + 2 + 1
필요한 거리에서 가장 큰 제곱수를 제외하고
남은 거리에서 필요한 횟수를 계산
#include <iostream>
#include <cmath>
using namespace std;
int isInteger(float num)
{
if (num / 1.00 == (int)num) {
//printf("We have an integer!\n");
return 1;
}
else {
//printf("We don't have an integer!\n");
return 0;
}
}
int main()
{
int T = 0;
cin >> T;
for (int i = 0; i < T; i++)
{
int start = 0;
int end = 0;
cin >> start;
cin >> end;
int count = 0;
float distance = end - start;
float root = sqrt(distance);
//cout << root << endl;
if(isInteger(root)){
cout << root * 2 - 1 << endl;
}else{
int j = floor(root);
distance -= j * j;
if (distance > j) {
cout << j * 2 - 1 + 2 << endl;
}
else {
cout << j * 2 - 1 + 1 << endl;
}
}
}
}