Skip to content

Pythagorus

Author: Pakin Olanraktham


View Problem Statement

Source: PROGRAMMING.IN.TH

Difficulty: Very Easy

Tags: Implementation

Prerequisites:

View External Solution

เฉลย

เฉลย

จาก Pythagorus จะได้ว่า \(c = \sqrt{a^2+b^2}\) ซึ่งสามารถเขียนโค้ดตรงๆ ได้เลย

หมายเหตุ

  • ต้องใช้ double เพื่อให้มีความแม่นยำของทศนิยมสูงขึ้น
  • หากเขียนในภาษา C ต้อง #include <math.h> เพื่อใช้ฟังก์ชั่น sqrt()
โค้ด
#include <stdio.h>
#include <math.h>

int main() {
    double a, b;
    scanf("%lf %lf", &a, &b);
    printf("%lf", sqrt(a*a + b*b));
}
#include <iostream>
#include <cmath>

using namespace std;

int main() {
    double a, b;
    cin >> a >> b;
    cout << sqrt(a*a + b*b);
}