4 solutions

  • 1
    @ 2025-7-18 19:21:10

    题目描述 给定三个正整数作为三条线段的长度,判断它们是否能构成一个三角形。构成三角形的条件是:任意两边之和大于第三边。

    解题思路 三角形判定条件:对于三条边 a, b, c,必须同时满足:

    a + b > c

    a + c > b

    b + c > a

    简化条件:若三条边已按非递减顺序排列(如 a ≤ b ≤ c),则只需判断 a + b > c 即可(因为 a + c > b 和 b + c > a 自然成立)。

    步骤:

    输入三个数,并排序。

    检查最小的两个数之和是否大于最大的数。

    AC代码:

    #include <iostream>
    #include <algorithm> // 用于 sort 函数
    using namespace std;
    
    int main() {
        int a, b, c;
        cin >> a >> b >> c;
        
        // 将三个数排序,方便比较
        int sides[3] = {a, b, c};
        sort(sides, sides + 3);
        
        // 检查最小的两边之和是否大于最大边
        if (sides[0] + sides[1] > sides[2]) {
            cout << 1 << endl;
        } else {
            cout << 0 << endl;
        }
        
        return 0;
    }
    
    • 1
      @ 2025-2-6 1:29:13

      if 判断,秒了

      #include <bits/stdc++.h>
      using namespace std;
      int a, b, c;
      int main() {
          cin >> a >> b >> c;
          if (a + b > c && a + c > b && b + c > a) cout << 1;
          else cout << 0;
          return 0;
      }
      
      • 0
        @ 2025-10-8 17:15:16
        #include <iostream>
        using namespace std;
        int main(){
        	int a,b,c;
        	cin>>a>>b>>c;
        	if(a+b>c&&a+c>b&&b+c>a){
        		cout<<1;
        	}else{
        		cout<<0;
        	}
        	return 0;
        }
        
        
        
        • -1
          @ 2024-12-12 18:57:51

          #include<bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin>>a>>b>>c; if (a+b>=c and a+c>=b and b+c>=a) {cout<<1;} else {cout<<0;} return 0; } //自己换行

          • 1

          Information

          ID
          4500
          Time
          1000ms
          Memory
          128MiB
          Difficulty
          1
          Tags
          (None)
          # Submissions
          125
          Accepted
          91
          Uploaded By