sliver__

백준 - Fly me to the Alpha Centauri(1011) 본문

CS/알고리즘

백준 - Fly me to the Alpha Centauri(1011)

sliver__ 2021. 11. 17. 23:44
728x90

https://www.acmicpc.net/problem/1011

 

1011번: Fly me to the Alpha Centauri

우현이는 어린 시절, 지구 외의 다른 행성에서도 인류들이 살아갈 수 있는 미래가 오리라 믿었다. 그리고 그가 지구라는 세상에 발을 내려 놓은 지 23년이 지난 지금, 세계 최연소 ASNA 우주 비행

www.acmicpc.net

Fly me to the Alpha Centauri

 

x, y 사이의 거리가 주어지고 몇번만에 갈 수 있는지 세는 문제이다.

한번에 이동을 a-1, a, a+1 만큼 움직일 수 있다.

처음은 0, 1만큼 움직일 수 있다.

 

x 방향으로부터 +(a+1)만큼, y 방향으로부터 -(a+1)만큼

거리를 좁혀나가며 답을 구했다

 

제출한 코드는 아래와 같습니다.

#include <iostream>
using namespace std;

int main(void)
{
	int t, x, y, dis,res;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		cin >> x >> y;
		dis = 1;
		res = 0;
		while (y - x > dis)
		{
			x += dis;
			y -= dis;
			res += 2;
			dis++;
		}
		if (y - x > 0) res++;
		cout << res << endl;
	}
}
728x90

'CS > 알고리즘' 카테고리의 다른 글

백준 - 소수(2581)  (0) 2021.11.18
백준 - 소수찾기(1978)  (0) 2021.11.18
백준 - 큰 수 A+B(10757)  (0) 2021.11.17
백준 - 부녀회장이 될테야(2775)  (0) 2021.11.17
ACM 호텔(10250)  (0) 2021.11.12
Comments