Around the Square

You are given a square and there are four circles which are overlapping the square. The center of ea…

Click here to read the complete problem statement.


If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.

a, r1, r2, r3, r4 = [int(a) for a in input().split()]
pi = 3.14159265
b = a * a
first  = pi * r1 * r1 / 4
second  = pi * r2 * r2 / 4
third  = pi * r3 * r3 / 4
forth  = pi * r4 * r4 / 4
total = first + second + third + forth
result = b - total
print("{:.3f}".format(result));

but saying wrong answer ,why??

try to find out the value pi(π ) pi=acos(-1)
@bengalsha

what’s wrong with this code??

#include<stdio.h>
#define pi 3.141592653589793;
int main()
{
    int a,l;
    float i;
    int r1,r2,r3,r4;
    double a1,a2,a3,a4;
    double d1,d2,d3,d4;
    scanf("%d ",&l);
    a=l*l;
    scanf("%d %d %d %d",&r1,&r2,&r3,&r4);
    a1=r1*r1*pi;
    a2=r2*r2*pi;
    a3=r3*r3*pi;
    a4=r4*r4*pi;
    d1=a1/4;
    d2=a2/4;
    d3=a3/4;
    d4=a4/4;
    i=a-(d1+d2+d3+d4);

    printf(" %.3f ",i);
    return 0;
}

Each test case can have multiple sets of input. You will have to take input until EOF (end of file).

#include<stdio.h>
#include<math.h>
int main()
{
	int a,r1,r2,r3,r4,b;
	float l,m,n,o,x,k;
	int y=-1;
	double z= acos(y);
	 while(scanf("%d%d%d%d%d",&a,&r1,&r2,&r3,&r4) != EOF);
	 {
	b=a * a;
	l=z * r1 * r1/4;
	m=z * r2 * r2/4;
	n=z * r3 * r3/4;
	o=z * r4 * r4/4;
	x=(l+m+n+o);
	k=b-x;
	if (r1+r2<=a && r2+r3<=a && r3+r4<=a && r4+r1<=a && r1+r3<=sqrt(2)*a && r2+r4<=sqrt(2)*a)
   {
	printf("%.3f\n",k);
	}
	else
	{
		printf("wrong input");
	}
	}
	return 0;
}

Why isn’t this accepted?

while(scanf("%d%d%d%d%d",&a,&r1,&r2,&r3,&r4) != EOF);

You have a semi-colon in this line. This is equivalent to writing this:

while(scanf("%d%d%d%d%d",&a,&r1,&r2,&r3,&r4) != EOF) { }

Here, you are taking all of the inputs, and producing the output for the last input only.

You can see your output for the sample case in your submission page:

So how can i produce outputs for all the inputs?

Remove the semi-colon. Make sure the rest of the code is within the while-loop.

#include<stdio.h>
#include<math.h>
int main()
{
	int a,r1,r2,r3,r4,b;
	float l,m,n,o,x,k;
	double pi= acos(-1);
	 while(scanf("%d%d%d%d%d",&a,&r1,&r2,&r3,&r4) != EOF){
	b=a * a;
	l=pi * r1 * r1/4;
	m=pi * r2 * r2/4;
	n=pi * r3 * r3/4;
	o=pi * r4 * r4/4;
	x=(l+m+n+o);
	k=b-x;
	if (r1+r2<=a && r2+r3<=a && r3+r4<=a && r4+r1<=a && r1+r3<=sqrt(2)*a && r2+r4<=sqrt(2)*a && a>0 && r1>=0 && r2>=0 && r3>=0 && r4>=0)
   {
	printf("%.3f\n",k);
	}
	else
	{
		printf("wrong input");
	}
	}
	return 0;
	
}

The first test case accepts the answer but the 2nd test case says wrong answer. Can you please indicate what is wrong in this program and how can I correct it?

You are missing “\n” at the end of “wrong input”.

I tried \n at the end of wrong input just now but 2nd case still shows wrong answer.
Is there any other mistake?

Please disregard my last response. I was under the assumption that the problem requires you to print “wrong input”. It doesn’t.

Here are the two things that you need to fix in your code:

  • You can always assume that the inputs will be within the range specified. You do not have to print anything other than what has been asked for in the output specification.

  • You should be using double precision for your calculation. Change the type of these variables: float l,m,n,o,x,k; to double.

1 Like

Thank you very much for your help.

2 Likes
#include <iostream>
#include <cmath>

#define PI acos(-1)

using namespace std;

int main(void) {
	unsigned int A, R1, R2, R3, R4;

	while(scanf("%d%d%d%d%d", &A, &R1, &R2, &R3, &R4) != EOF) {
		double diagonal = sqrt(2 * A * A) / 2;

		if( (((R1 + R2) <= A) && ((R2 + R3) <= A) && ((R3 + R4) <= A)) && ((R1 <= diagonal) && (R2 <= diagonal) && (R3 <= diagonal) && (R4 <= diagonal)) ) {
			int area_A = A * A;

			double area_R1 = (R1 * R1 * PI) / 4,
				area_R2 = (R2 * R2 * PI) / 4,
				area_R3 = (R3 * R3 * PI) / 4,
				area_R4 = (R4 * R4 * PI) / 4;

			printf("%.3f\n", (area_A - (area_R1 + area_R2 + area_R3 + area_R4)));
		}
	}

	return 0;
}

What is wrong with the code??? Am I missing something?? It’s showing Wrong Answer on 2nd test.

For this problem, It has been assumed (although not mentioned) that the circles will not overlap each other. So, remove the following line,

if( (((R1 + R2) <= A) && ((R2 + R3) <= A) && ((R3 + R4) <= A)) && ((R1 <= diagonal) && (R2 <= diagonal) && (R3 <= diagonal) && (R4 <= diagonal)) ) {

@hjr265, I think you should update the problem statement.

#include<bits/stdc++.h>
using namespace std;
int main()
{
   long double a,r1,r2,r3,r4;
   cout.precision(3);cout<<fixed;
   while(!(cin>>a>>r1>>r2>>r3>>r4).eof())
   {


       cout<<a*a-(acos(-1)/4)*(r1*r1+r2*r2+r3*r3+r4*r4)<<endl;
   }
   return 0;

what’s wrong?

#include <stdio.h>
#include <math.h>

int main() {
    float a,b,r1,r2,r3,r4,r,ans;
    char c;

    while(scanf("%f %f %f %f %f",&a,&r1,&r2,&r3,&r4)!= EOF){
    //c=getch();
        //
        a=a*a;
        r1=(acos(-1)*(r1*r1))/4;
        r2=(acos(-1)*(r2*r2))/4;
        r3=(acos(-1)*(r3*r3))/4;
        r4=(acos(-1)*(r4*r4))/4;
        r=r1+r2+r3+r4;

        ans=a-(r1+r2+r3+r4);
        printf("%.3f\n",ans);

    }


}

whats wrong, WA in test case 2

What’s Wrong?

#include <stdio.h>

int main()
{
  double a,b,c,d,e,f,g,h,i,j;
  while(getchar() != EOF){
  scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e);

  g = ((b*b)+(c*c)+(d*d)+(e*e))*3.1416/4;
  h = (a*a)-g;
  printf("%0.3lf\n",h);
}
  return 0;
}

why is it giving wrong answer in case2?

while True:
	try:
		A,B,C,D,E=input().split()
		pi=3.14159
		a=int(A)*int(A)
		b=0.25*int(B)*int(B)*pi
		c=0.25*int(C)*int(C)*pi
		d=0.25*int(D)*int(D)*pi
		e=0.25*int(E)*int(E)*pi
		area=a-b-c-d-e
		print("%0.3f"%area)
	except EOFError:
		break