UVA Solution 356 - Square Pegs And Round Holes - Solution in C, C++ | Volume 3 - 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

Friday, May 5, 2017

UVA Solution 356 - Square Pegs And Round Holes - Solution in C, C++ | Volume 3

UVA Solution 356 - Square Pegs And Round Holes - Solution in C, C++ | Volume 3


UVA Online Judge Solution 356 - Square Pegs And Round Holes | Volume 3
UVA Problem Link - https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=show_problem&problem=292

Problem Name: 356 - Square Pegs And Round Holes
Problem Number : UVA - 356 - Square Pegs And Round Holes
Online Judge : UVA Online Judge Solution
Volume: 3
Solution Language : C, C plus plus

UVA Solution 356 - Square Pegs And Round Holes - Solution in C, C++ | Volume 3

UVA Solution 356 - Square Pegs And Round Holes Code in C, CPP:


#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;
}

No comments:

Post a Comment