zoukankan      html  css  js  c++  java
  • codeforces157B

    Trace

     CodeForces - 157B 

    One day, as Sherlock Holmes was tracking down one very important criminal, he found a wonderful painting on the wall. This wall could be represented as a plane. The painting had several concentric circles that divided the wall into several parts. Some parts were painted red and all the other were painted blue. Besides, any two neighboring parts were painted different colors, that is, the red and the blue color were alternating, i. e. followed one after the other. The outer area of the wall (the area that lied outside all circles) was painted blue. Help Sherlock Holmes determine the total area of red parts of the wall.

    Let us remind you that two circles are called concentric if their centers coincide. Several circles are called concentric if any two of them are concentric.

    Input

    The first line contains the single integer n (1 ≤ n ≤ 100). The second line contains n space-separated integers ri (1 ≤ ri ≤ 1000) — the circles' radii. It is guaranteed that all circles are different.

    Output

    Print the single real number — total area of the part of the wall that is painted red. The answer is accepted if absolute or relative error doesn't exceed 10 - 4.

    Examples

    Input
    1
    1
    Output
    3.1415926536
    Input
    3
    1 4 2
    Output
    40.8407044967

    Note

    In the first sample the picture is just one circle of radius 1. Inner part of the circle is painted red. The area of the red part equals π × 12 = π.

    In the second sample there are three circles of radii 1, 4 and 2. Outside part of the second circle is painted blue. Part between the second and the third circles is painted red. Part between the first and the third is painted blue. And, finally, the inner part of the first circle is painted red. Overall there are two red parts: the ring between the second and the third circles and the inner part of the first circle. Total area of the red parts is equal (π × 42 - π × 22) + π × 12 = π × 12 + π = 13π

    sol:小学奥数,为了防止丢精度,把R求出来,只用一次π。

    #include <bits/stdc++.h>
    using namespace std;
    typedef int ll;
    inline ll read()
    {
        ll s=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0'); return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    const int N=105;
    const double Pi=3.141592653589793238;
    int n;
    double r[N];
    int main()
    {
        int i;
        double R=0;
        R(n);
        for(i=1;i<=n;i++) scanf("%lf",&r[i]);
        sort(r+1,r+n+1);
        for(i=n;i>=1;i-=2)
        {
            R+=r[i]*r[i];
        }
        for(i=n-1;i>=1;i-=2)
        {
            R-=r[i]*r[i];
        }
        printf("%.10lf
    ",Pi*R);
        return 0;
    }
    /*
    input
    1
    1
    output
    3.1415926536
    
    input
    3
    1 4 2
    output
    40.8407044967
    */
    View Code
  • 相关阅读:
    (TOJ1004)渊子赛马
    (TOJ1063)养兔子
    (TOJ3260)Palindromes
    (TOJ1070)Least Common Multiple
    (TOJ1215)数据结构练习题——合并表
    (TOJ1481)C语言实验题——鞍点
    (TOJ1496)C语言实验题——字符过滤
    (TOJ1003)1、2、3、4、5...
    (TOJ1490)C语言实验题——合法的C标识符
    (TOJ2804)Even? Odd?
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10633030.html
Copyright © 2011-2022 走看看