[C Language Elementary] How to use C language to judge what kind of triangle a set of numbers is (simple! easy to understand!)

Question stem

KiKi wants to know whether the given three sides a, b, c can form a triangle, and if it can form a triangle, determine the type of triangle (equilateral triangle, isosceles triangle or ordinary triangle).

Enter a description:

There are multiple sets of input data for the topic, and each line enters three a, b, c (0<a, b, c<1000) as the three sides of the triangle, separated by spaces.

Output description:

For each set of input data, the output occupies one line. If a triangle can be formed, the equilateral triangle will output "Equilateral triangle!", the isosceles triangle will output "Isosceles triangle!", and the rest of the triangles will output "Ordinary triangle!", and vice versa.

example

enter:

2 3 2
3 3 3

output:

Isosceles triangle!
Equilateral triangle!

solution

First sight

  1. equilateral triangle three equal sides
  2. Isosceles triangle with two equal sides
  3. A triangle is only true if the sum of two sides is less than or equal to the third side

First look at the code

#include <stdio.h>

int main() {
    
    
    int a,b,c=0;
    while(scanf("%d %d %d",&a,&b,&c)!=EOF)
    //while这里是一个多组输入的写法,可以重复输入不同的三角形,与本题题干关系不大,故不深入研究
    {
    
    
    if (a==b && b==c)
    	//运行符&&表示且,即条件中必须所有同时成立才ok
	    printf("Equilateral triangle!\n");
    else if (a==b || b==c || a==c)
        //运行符||表示或,即条件中有一个条件是成立的就ok
	    printf("Isosceles triangle!\n");
    else if (a+b<=c || a+c<=b || b+c<=a)
	    printf("Not a triangle!\n");
    else
	    printf("Ordinary triangle!\n");}
    return 0;
}

operation result

image.png

  • Obviously, there is an error in the last set of data , and the output that should have been "Not a triangle!" has become "Isosceles triangle!" At this point, we need to adjust our code thinking :

Change of thinking

We reversed the order of ideas 1 and 3 :

  1. A triangle is only true if the sum of two sides is less than or equal to the third side
  2. equilateral triangle three equal sides
  3. Isosceles triangle with two equal sides

code change

#include <stdio.h>

int main() {
    
    
    int a,b,c=0;
    while(scanf("%d %d %d",&a,&b,&c)!=EOF)
    {
    
    
    if (a+b<=c || a+c<=b || b+c<=a)
	    printf("Not a triangle!\n");
    //原本在最下面的判断三角形移到了这里
    else if (a==b && b==c)
	    printf("Equilateral triangle!\n");
    else if (a==b || b==c || a==c)
	    printf("Isosceles triangle!\n");
    else
	    printf("Ordinary triangle!\n");
	    }
    return 0;
}
  • Obviously, as soon as the modified idea comes up, it first judges whether the group of numbers is a triangle. If it is, continue to judge later, if not, it will output directly.

operation result

image.png
Successfully run~~

write at the end

If this article is helpful to you, can you give me a little like ❤~ Your support is my biggest motivation.

The blogger is Xiaobai, with little talent and learning, and it is inevitable that there will be mistakes. Everyone is welcome to discuss and ask questions, and the blogger will correct it as soon as possible.

Thank you for watching hehe (๑•̀ㅂ•́)و✧~!

Guess you like

Origin blog.csdn.net/weixin_70218204/article/details/130873691