Skip to content

Herman

Author: Pakin Olanraktham & Black Cat


View Problem Statement

Source: PROGRAMMING.IN.TH

Difficulty: ?

Tags: Implementation

Prerequisites:

เฉลย

เฉลย

การคิดพื้นที่ตามเรขาคณิตทั่วไป เราสามารถใช้สูตรการหาพื้นที่วงกลมได้เลย ซึ่งก็คือ \(\pi\dot r^{2}\) ซึ่งเราสามารถเรียกใช้ค่า \(\pi\) ได้จาก 'M_PI' จาก header math.h หรือ cmath ได้โดยตรง ในส่วนของการคิดพื้นที่ตามแบบเรขาคณิดรถแท็กซี่ หากเราลองวาดพื้นที่ที่จะได้จากการคำนวณรัศมีในแบบนี้ออกมาแล้ว เราจะได้ออกมาเป็นรูปสี่เปลี่ยมข้าวหลามตัด ที่มีเส้นทแยงมุมยาว \(2r\) ซึ่งคำนวณพื้นที่ได้ด้วย \(\frac{1}{2}\dot 2r\dot 2r = 2\dot r\dot r\)

โค้ด
#include <stdio.h>
#include <math.h>

int main() {
    int r;

    scanf("%d", &r);

    printf("%lf\n%lf", M_PI*r*r, r*r*2);
}
#include <iostream>
#include <cmath>

using namespace std;

int main() {
    double r;

    cin >> r;

    cout << M_PI*r*r << "\n" << r*r*2;
}