๐Ÿ“ Coding Test Study/Math

[Math&Algorithm][C++] ์œ ํด๋ฆฌ๋””์•ˆ ๊ฑฐ๋ฆฌ(Euclidean Distance)

ibelieveinme 2021. 8. 9. 03:02
728x90

๋‘ ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” '์œ ํด๋ฆฌ๋””์•ˆ ๊ฑฐ๋ฆฌ'๊ณต์‹ !

 

๋‘ ์  (x1, y1), (x2, y2)์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‘ ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋Š” ๋‹ค์Œ ๊ณต์‹์œผ๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

#include <iostream>
#include <cmath> // sqrt()์™€ pow() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด

using namespace std;

int main(){
	int x1, y1, x2, y2;
    double distance;
    
    cin >> x1 >> y1 >> x2 >> x2;
    distance = sqrt(pow(x2-x1,2) + pow(y2 - y1, 2));
    cout << distance;
    
	return 0;
}

 

โ‘  pow(): n ์ œ๊ณฑ์„ ๊ตฌํ•˜๊ธฐ

โ‘ก sqrt(): ๋ฃจํŠธ ๊ตฌํ•˜๊ธฐ

728x90

'๐Ÿ“ Coding Test Study > Math' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Big-O ํ‘œ๊ธฐ๋ฒ•  (0) 2021.04.25
[Math&Algorithm] ์†Œ์ˆ˜?  (0) 2021.03.01
[Math&Algorithm] ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜? ์ตœ๋Œ€๊ณต๋ฐฐ์ˆ˜?  (0) 2021.03.01