MYSQL中的三角形类型

时间:2016-07-25 07:28:30

标签: mysql

问题陈述:

  

编写一个查询,以识别TRIANGLES中每条记录的类型   桌子使用其三个边长。输出以下之一   表中每条记录的陈述:

     
      
  • 不是三角形:A,B和C的给定值不形成三角形。
  •   
  • 等边:这是一个边长相等的三角形。
  •   
  • Isosceles:这是一个两边长度相等的三角形。
  •   
  • Scalene:这是一个边长不同的三角形。输入格式
  •   
     

TRIANGLES表描述如下:

     

表中的每一行表示每个三角形的长度   三方面。

Sample Input 
------------
A  B  C  
20 20 23 
20 20 20  
20 21 22  
13 14 30

Sample Output
-------------
Isosceles
Equilateral
Scalene
Not A Triangle

尝试不起作用:

select 
 case 
  when A+B < C or A+C < B or B+C < A then "Not A Triangle"
  when A=B and B=C then "Equilateral"
  when A=B or A=C or B=C then "Isosceles"  
  when A<>B and B<>C then "Scalene" 


 end as triangles_type 
from TRIANGLES; 

20 个答案:

答案 0 :(得分:8)

select case
when A+B <= C or A+C <= B or B+C <= A then "Not A Triangle"
when A=B and B=C then "Equilateral"
when A=B or A=C or B=C then "Isosceles"
else "Scalene"
end as triangles_type
from TRIANGLES;

答案 1 :(得分:5)

SELECT
  CASE 
    WHEN A + B <= C or A + C <= B or B + C <= A THEN 'Not A Triangle'
    WHEN A = B and B = C THEN 'Equilateral'
    WHEN A = B or A = C or B = C THEN 'Isosceles'
    WHEN A <> B and B <> C THEN 'Scalene'
  END tuple
FROM TRIANGLES;
  1. 使用case语句检查给定输入是否为三角形。
  2. 如果是三角形,则检查所有边是否相同如果为真三角形类型为&#39; Equilateral&#39;。
  3. 如果不是,如果 true 检查是否有任何双方是相等的,则三角形类型是&#39; Isosceles&#39;
  4. 在不相等的情况下,三角形类型的任何一边都是“Scalene”。我们也可以直接使用 ELSE

答案 2 :(得分:3)

SELECT  CASE
    WHEN A+B>C AND B+C>A AND A+C>B THEN
        CASE 
            WHEN A=B AND B=C THEN 'Equilateral'
            WHEN A=B or A=C OR B=C THEN 'Isosceles'
            ELSE 'Scalene'
        END
    ELSE 'Not A Triangle'
END
FROM TRIANGLES 
  • 步骤1:首先我们选择案例来检查给定输入是否为三角形

  • 步骤2:如果是三角形,则检查其类型。所以第一种情况如果所有边都相同,那么三角形类型是'Equilateral',那么第二种情况如果任何两边相同则三角形类型是'Isosceles',否则三角形类型是{{1 }}

答案 3 :(得分:2)

SELECT
CASE
    WHEN (A > 0 AND B > 0 AND C > 0 AND (A + B > C) AND (B + C > A) AND (A + C > B)) 
    THEN
    (
        CASE
            WHEN (A = B AND B = C) THEN 'Equilateral'
            WHEN (A = B OR B = C OR A = C) THEN 'Isosceles'
            ELSE 'Scalene'
        END
    )
    ELSE 'Not A Triangle'
END
FROM TRIANGLES;

答案 4 :(得分:1)

对于Oracle 11g,这是有效的:

SELECT CASE WHEN A+B>C then case when A=B AND B=C THEN 'Equilateral'
   WHEN A=B OR B=C OR A=C THEN 'Isosceles'
   WHEN A!=B or B!=C OR A!=C THEN 'Scalene' END
   ELSE 'Not A Triangle' END
   FROM TRIANGLES;

答案 5 :(得分:1)

{{1}}

这里,它会严格检查三角形的类型。

答案 6 :(得分:1)

您的答案实际上是正确的。但是在以下问题上缺少一个(三角形的“正确的定义”)的信息(这不是您的错),即如果双方的和大于或等于另一方,则不是三角形

您唯一需要做的就是将'='放在'<'

附近
select 
 case 
  when A+B <= C or A+C <= B or B+C <= A then "Not A Triangle"
  when A=B and B=C then "Equilateral"
  when A=B or A=C or B=C then "Isosceles"  
  when A<>B and B<>C then "Scalene" 


 end as triangles_type 
from TRIANGLES; 

答案 7 :(得分:0)

尝试下面提到的查询。这肯定会奏效。

SELECT
  CASE
    WHEN A + B > C AND A+C>B AND B+C>A THEN 
      CASE
        WHEN A = B AND B = C THEN 'Equilateral' 
        WHEN A = B OR B = C OR A = C THEN 'Isosceles' 
        ELSE 'Scalene' 
      END 
    ELSE 'Not A Triangle' 
  END FROM TRIANGLES;

说明:

首先检查这些值是否满足三角形的要求(即两条边之和必须大于另一边)

如果上述条件成立,则寻找三角形的类型

  • 等边 - 所有边都相等
  • 等腰线 - 任何两条边都相等
  • Scalene - 所有三个方面都不同

如果上述条件为假,则

  • 不是三角形

答案 8 :(得分:0)

scipy_kurtosis()

答案 9 :(得分:0)

通过了所有测试用例的MySQL:-

SELECT CASE 
WHEN A + B > C AND A+C>B AND B+C>A THEN CASE 
WHEN A = B AND B = C THEN 'Equilateral' 
WHEN A = B OR B = C OR A = C THEN 'Isosceles' 
WHEN A != B OR B != C OR A != C THEN 'Scalene' 
END ELSE 'Not A Triangle' END FROM TRIANGLES;

答案 10 :(得分:0)

MySQL:

select
case 
    when ((A+B)<=C) || ((C+B)<=A) || ((A+C)<=B)  then 'Not A Triangle'
    when A=B && B=C && A=C then 'Equilateral'
    when ((A=B) && (A+B)>C) || ((B=C) && (B+C)>A)  then 'Isosceles'    
    when A!=B && B!=C && A!=C then 'Scalene' 
    end
from TRIANGLES;

说明:

首先,检查值是否满足三角形的要求(即,两个边的总和必须大于另一边)-非三角形

然后,寻找三角形的类型

  1. 等边-各方平等
  2. 等腰线-任何两个边都是相等的(包括两个边的和大于另一个边的附加条件-当我们检查三角形的基本规则作为第一种情况时,可以忽略不计)< / li>
  3. Scalene -所有三个方面都不同

答案 11 :(得分:0)

/ *下面的代码将检查三角形的所有可能情况,此代码将100%起作用* /

select case
when A+B<=C OR A+C<=B OR C+B<=A THEN "Not A Triangle"
when (A<>B AND A+B>C) AND (B<>C AND B+C>A) AND (A<>C AND A+C>B) THEN 
"Scalene"
when (A=B AND B<>C AND A+B>C) OR (B=C AND B<>A AND B+C>A) OR (A=C AND C<>B 
AND A+C>B) THEN "Isosceles"
when A=B AND B=C THEN "Equilateral"
end as triangle
from TRIANGLES;

答案 12 :(得分:0)

对于MySQL:

select
  case
      when (a+b>c) and (a+c>b) and (b+c>a) then
           case
              when (a=b) and (b=c) and (a=c) then 'Equilateral'
              when (a=b) or (b=c) or (a=c) then 'Isosceles'
              else 'Scalene'
           end
       else 'Not A Triangle'
    end    
from TRIANGLES 

答案 13 :(得分:0)

选择     案件         当a = b和b = c时,则为“等边”         当a + b <= c或b + c <= a或a + c

答案 14 :(得分:0)

SELECT
CASE
    WHEN (A+B>C AND B+C>A AND A+C>B) THEN
    (CASE
     WHEN A=B AND B=C AND A=C THEN 'Equilateral'
     WHEN A=B OR B=C OR A=C THEN 'Isosceles'
     ELSE 'Scalene'
     END)
    ELSE 'Not A Triangle'
END
FROM TRIANGLES;

答案 15 :(得分:0)

SELECT CASE
WHEN (A + B <= C) OR (B+C <= A) OR (A+C <= B) THEN "Not A Triangle" 
WHEN (A=B) AND (B=C) THEN "Equilateral"
WHEN (A=B) OR (C=A) OR (B=C) THEN "Isosceles" 
ELSE "Scalene" 
END 
FROM TRIANGLES

答案 16 :(得分:-1)

选择IF(A + B&gt; C和A + C&gt; B和B + C&gt; A,IF(A = B和B = C,'等边',IF(A = B或B = C或A =来自三角形的C,'Isosceles','Scalene')),'不是三角';

答案 17 :(得分:-1)

select 
case
    when (a+b>c) and (b+c>a) and (c+a>b) then
        case
            when a=b and b=c then 'Equilateral'
            when a=b or b=c or c=a then 'Isosceles'
            else 'Scalene'
        end
    else 'Not A Triangle'
end 
from triangles;

答案 18 :(得分:-1)

select CASE WHEN ((A+B)<=C OR (B+C)<=A OR (A+C)<=B ) THEN 'Not A Triangle'
       ELSE (case when (A=B) AND (B=C) AND (A=C) THEN 'Equilateral'
        WHEN (A=B) OR (B=C) OR (A=C) THEN 'Isosceles'
        WHEN (A<>B) AND (B<>C) AND (A<>C) THEN 'Scalene'
        END )
        END TYPET
FROM TRIANGLES

1. CHECK IF IT IS NOT A TRIANGLE
2. ELSE CHECK IF ALL SIDES ARE EQUAL THEN  'Equilateral'
3. IF ONLY TWO SIDES ARE EQUAL THEN Isosceles
4. IF NO SIDES ARE EQUAL THEN 'Scalene'

答案 19 :(得分:-1)

这个答案在sql * plus中运行良好,但在hackerrank中却不行,我不知道为什么?试试这个。

declare
 cursor mytriangle is select A,B,C from triangles;
 a triangles.A %type;
 b triangles.B %type;
 c triangles.C %type;
 begin
 open mytriangle;
 if mytriangle%isopen then
 loop
 fetch mytriangle into a,b,c;
 exit when mytriangle %notfound;
 case
 when a+b<=c or b+c<=a or a+c<=b then
 dbms_output.put_line('Not A Triangle');
 when a=b and b=c then
 dbms_output.put_line('Equilateral');
 when a=b or b=c or c=a then
 dbms_output.put_line('Isoceles');
 when a+b>=c and b+c>=a and a+c>=b and a!=b and b!=c and c!=a then
 dbms_output.put_line('Scalene');
 else
 dbms_output.put_line('');
 end case;
 end loop;
 close mytriangle;
 else dbms_output.put_line('cannot open the cursor');
 end if;
end;