UVA356 LA5250 Square Pegs And Round Holes计算几何

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

以上是关于UVA356 LA5250 Square Pegs And Round Holes计算几何的主要内容,如果未能解决你的问题,请参考以下文章

UVA - 1533 - Moving Pegs

codeforces 356C Bear and Square Grid

UVA - 11461-Square Numbers

UVA11470 Square Sums水题

UVA 11461 Square Numbers解题报告

[2016-03-19][UVA][11520][Fill the Square]