Triangle


This is a companion discussion topic for the original entry at https://toph.co/p/triangle
t = int(input())
for i in range(0,t):
	a,b,c=map(int,input().split())
	s = (a+b+c)/2
	area =(s * (s-a) * (s-b) * (s-c))**(1/2)
	if a+b>=c and b+c>=a and c+a>=b:
		print("%.2f"%area)
	else:
		
		print("Oh, No!")

this doesn’t create triangle. IT’S A STRAIGHT LINE. DO THEY KNOW MATH ?

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

int main()
{
    int i,n, a,b,c;
    float s=0.0,area=0.0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d %d %d",&a,&b,&c);
        if((a+b)>c&&(b+c)>a&&(c+a)>b)
        {
            s=(float)(a+b+c)/2;
            area=sqrt(s*(s-a)*(s-b)*(s-c));
            printf("%.2f\n",area);
        }
        else
        {
            printf("Oh, No!");
        }
    }
    return 0;
}

what’s the problem in my code…anybody please help…

what’s problem in this code:

a = int(input())
b = int(input())
c = int(input())
if (a+b) > c and (a+c) > b and (b+c) > a:
s = (a + b + c) / 2
area = (s*(s-a)(s-b)(s-c))**0.5
print("{0:.2f}".format(float(area)))
else:
print(“Oh, No!”)

Change the data type of a,b,c to double or float.

1 Like

https://www.google.com/search?client=firefox-b-d&q=the+correct+theory+to+form+a+triangle no man you are wrong

The test cases are WRONG for this problem!
I think, problem setter forgot that (a+b >= c && a+c >= b && c+a >= b) DOESN’T form a triangle. It must be (a+b > c && a+c > b && c+a > b) for a triangle.

1 Like

#include <bits/stdc++.h>

using namespace std;

int main(){
int t;
cin>>t;
while(t–) {
int a,b,c;
cin>>a>>b>>c;
if(a+b>c && b+c>a && c+a>b) {
double s=(double)(a+b+c)/2;
double area=sqrt(s*(s-a)(s-b)(s-c));
cout<<fixed<<setprecision(2)<<area<<endl;
}
else cout<<“Oh, No!”<<endl;
}
}

What’s the problem in this code? It gets wrong ans in test 2. Why?!

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int main()
{
	int N;
	double s;
	double area;
	std::cin >> N;
	std::vector<int> v(N);
	int &r = v[0];
	int &r1 = v[1];
	int &r2 = v[2];
	for(int i = 0; i < N; i++)
	{
		int c,a,b;
		std::cin >> a >>b>>c;
		
		if(a>0&&b>0&&c>0)
		{
			r = a;
			r1 = b;
			r2 = c;
		
			s = (v[0] + v[1] + v[2]) * 0.5;
		
			area = sqrt(s * (s-v[0]) * (s-v[1]) * (s-v[2]));
			std::cout << area <<endl;
		}else{
			std::cout << "Oh,No!";
		}
	}
	
}

Why is this code getting wrong answer?

Cause your logic ain’t right, just try to learn the conditions that makes a triangle.
Just having sides > 0 does not form a triangle.
For example, can you form a triangle with sides with length 7, 2 and 12 ?
try it yourself and then rethink the logic.

#include    <stdio.h>
#include    <math.h>
int main()
{
    float a,b,c,s,area;
    scanf("%f%f%f",&a,&b,&c);
    if((a+b <   c)  ||  (a+c <   b) ||  (b+c    <   a)){
        printf("Oh,No!");
    }
    else{
        s=(a+b+c)/2;
        area    =   sqrt(s*(s-a)*(s-b)*(s-c));
        printf("%.2f",area);
    }
 

   return  0;
}

why my code is wrong in toph???

@ProgrammarSk use <= instead of <here.
0 is also considered as a valid result here.
cause it satisfies the equation area = sqrt(s(s-a)(s-b)(s-c))
and also i wonder how you code is even compiling?
you have to put asterisk * whereever you perform multiplication in a c/c++ program.
so you have to write:

area = sqrt(s*(s-a)*(s-b)*(s-c));

Toph.co shows this code wrong…why??:sleepy:

#include <math.h>
 int main()
 { float a,b,c,s,area; 
scanf("%f%f%f",&a,&b,&c);
 if((a+b <=   c) || (a+c <= b) || (b+c <= a)){ printf(“Oh,No!”); 
} 
else{ 
s=(a+b+c)/2;
 area = sqrt(s*(s-a)*(s-b)*(s-c));
 printf("%.2f",area);
 } 
return 0;
 }

There are many test cases, you are printing answere for only one.

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

int main()
{
	int N, a, b, c, i;
	scanf("%d", &N);
	
	double area, s;
	
	for(i = 1; i <= N; i++) {
		scanf("%d %d %d", &a, &b, &c);
		
		if(a+b>c && a+c>b && b+c>a) {
			s = (a + b + c) / 2;
			area = sqrt(s * (s-a) * (s-b) * (s-c));
			
			printf("%.2lf\n", area);
		}
		
		else {
			printf("Oh, No!\n");
		}
	}
	
	return 0;
}

WHAT IS THE PROBLEM IN THIS CODE?
IT IS SHOWING WRONG ANSWER IN TEST CASE 2.

Use >= sign instead of >

I think the test case 2 should be updated. Because we know sum of two sides of a triangle must be greater than the third side. I submitted my code with this theory but I got wrong answer. But when I submitted my code with the wrong theory I got AC.
The wrong theory:
a+b>=c
a+c>=b
b+c>=a
:expressionless:

1 Like

Anything that returns positive real number in the equation s = root(s * (s - a) * (s - b) * (s - c)) has been considered the sides of a correct triangle for this problem.

@Soudip Thanks for pointing this out. Apparently this problem was authored by an external author, and I never really got a chance to review it myself.

I will get the dataset updated.

I tried it one year before. Still failed. Help me someone. Wrong ans on case 2. Tried with > and now trying with >= but no result

#include <stdio.h>
#include <math.h>
int main()
{
    int a, i;
    scanf("%d", &a);
    if(a<0)
    {
        printf("\"Oh,No!\"\n");
    }
    for(i=1; i<=a; i++)
    {
        double a, b, c, s, area;
        scanf("%lf%lf%lf", &a, &b, &c);
        if(a+b>=c && a+c>=b && b+c>=a && a!=0 && b!=0 && c!=0 && a>0 && b>0 && c>0) {
        s=(a+b+c)/2;
        printf("%0.2lf\n", sqrt(s*(s-a)*(s-b)*(s-c)));
        }
        else {
            printf("\"Oh,No!\"\n");
        }
    }
    return 0;
}