UVA Solution 412 - Pi - Solution in C | Volume 4 - Online Judge Solution

Latest

It is a free Online judges problems solution list. Here you can find UVA online Judge Solution, URI Online Judge Solution, Code Marshal Online Judge Solution, Spoz Online Judge Problems Solution

Sunday, May 7, 2017

UVA Solution 412 - Pi - Solution in C | Volume 4

UVA Solution 412 - Pi - Solution in C | Volume 4


UVA Online Judge Solution 412 - Pi | Volume 4
UVA Problem Link - https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=353

Problem Name: 412 - Pi
Problem Number : UVA - 412 - Pi
Online Judge : UVA Online Judge Solution
Volume: 4
Solution Language : C
UVA Solution 412 - Pi - Solution in C | Volume 4

UVA Solution 412 - Pi Code in C:


#include <stdio.h>
#include <math.h>
int gcd(int x, int y) {
    int t;
    while(x%y) {
        t = x, x = y, y = t%y;
    }
    return y;
}
int main() {
    int n, A[50], i, j;
    while(scanf("%d", &n) == 1 && n) {
        for(i = 0; i < n; i++)
            scanf("%d", &A[i]);
        int s = n*(n-1)/2, cnt = 0;
        for(i = 0; i < n; i++) {
            for(j = i+1; j < n; j++)
                if(gcd(A[i], A[j]) == 1)
                    cnt++;
        }
        if(!cnt)
            puts("No estimate for this data set.");
        else {
            double pi = sqrt((double)6*s/cnt);
            printf("%.6lf\n", pi);
        }
    }
    return 0;
}

No comments:

Post a Comment