题解:AT_past202306_c 円の描画

· · 题解

思路

模拟题意,运用两点间距离公式判断当前点到圆心的距离与圆半径的关系即可。

两点间距离公式:设点 A(x_1,y_1),B(x_2,y_2),则 AB = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}。

Code

#include <bits/stdc++.h>
using namespace std;
int x,y,r,n;
signed main() {
    cin>>x>>y>>r>>n;
    for (int i=-n;i<=n;i++) {
        for (int j=-n;j<=n;j++) {
            if (sqrt((i-x)*(i-x)+(j-y)*(j-y))<=r) cout<<"# ";
            else cout<<". ";
        }
        cout<<endl;
    }
    return 0;
}

AC 记录