UVA356 LA5250 Square Pegs And Round Holes【计算几何】

A circle 2n − 1 units in diameter has been drawn centered on a 2n by 2n chessboard. The construction for n = 3 is illustrated below.
在这里插入图片描述

    Write a program that will determine the number of cells of the board which contain a segment of the circle and the number of cells of the board which lie entirely inside the circle.
Input
Each line of the input file will contain a positive integer no greater than 150.
Output
For each input value n, write two statements on consecutive lines of the output file in the format indicated in the sample output. Follow this with a blank line to separate your output for successive inputs.
Sample input
3
4
Sample output
In the case n = 3, 20 cells contain segments of the circle.
There are 12 cells completely contained in the circle.

In the case n = 4, 28 cells contain segments of the circle.
There are 24 cells completely contained in the circle.

问题链接UVA356 LA5250 Square Pegs And Round Holes
问题简述:(略)
问题分析:简单的计算几何问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA356 LA5250 Square Pegs And Round Holes */

#include <bits/stdc++.h>

using namespace std;

const int N = 150;
int ans[N + 1];

int main()
{
    for (int i = 1; i <= N; i++) {
        double r = i - 0.5;
        int sum = 0;
        for (int x = 1; x <= i; x++)
            for (int y = 1; y <= i; y++)
                if (x * x + y * y < r * r) sum++;
        ans[i] = 4 * sum;
    }

    int n, flag = 0;
    while (~scanf("%d", &n)) {
        if (flag) printf("\n");
        flag = 1;
        printf("In the case n = %d, %d cells contain segments of the circle.\n", n, 8 * n - 4);
        printf("There are %d cells completely contained in the circle.\n", ans[n]);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值