24h購物| | PChome| 登入
2012-05-19 07:28:46| 人氣1,029| 回應0 | 上一篇 | 下一篇

[UVA][Math] 356 - Square Pegs And Round Holes

推薦 0 收藏 0 轉貼0 訂閱站台


 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 and Output

Each line of the input file will contain a positive integer no greater than 150. For each input value n, write two statements on consecutive lines of the output file in the format indicated in the sample output. Print a blank line between successive outputs.

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.

原本想用 O(150*150) 建表的, 後來發現有點懶了, 並不好寫

#include <stdio.h>
#include <math.h>
int main() {
    int n, i, j, flag = 0;
    while(scanf("%d", &n) == 1) {
        if(flag)
            puts("");
        flag = 1;
        int t1 = 0, t2 = 0;
        double r = n-0.5, r1, r2;
        for(i = 0; i < n; i++) {
            for(j = 0; j < n; j++) {
                r1 = sqrt(i*i+j*j);
                r2 = sqrt(i*i+j*j+2*i+2*j+2);
                if(r1 <= r && r2 <= r)
                    t1++;
                if(r1 <= r && r2 > r)
                    t2++;
            }
        }
        printf("In the case n = %d, %d cells contain segments of the circle.\n", n, t2*4);
        printf("There are %d cells completely contained in the circle.\n", t1*4);
    }
    return 0;
}

台長: Morris
人氣(1,029) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][分析字串] 587 - There's treasure everywhere!
此分類上一篇:[UVA][Math] 378 - Intersecting Lines

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文