使用函数判断三角形(10分)【简单判断 IF 】

使用函数判断三角形(10分)

Description

给定n个可选的木棍长度,问是否能够选出3个构成三角形。

注意,你需要实现一个函数ok,来判断传入的三个边长能否构成三角形。

函数接口定义:

int isTriangle( int x, int y, int z);

传入三个边长参数x,y,z,是三角形返回1,否则0。

Input

第一行一个整数n。(3<=n<=20)

第二行n个整数,代表木棍长度a[i]。(1<=a[i]<=10^8)

Output

若能够选出3个构成三角形,输出最小的三角形周长,否则输出“No”。

Sample Input 1 

3
1 2 3

Sample Output 1

No
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <typeinfo.h>
//#include <climits>
//#include <iostream>
//#include <cstdio>
//#include <cstring>
//#include <bits/stdc++.h>
//#include <queue>
//#include <algorithm>
//#include <map>
//#include <cstdlib>
//using namespace std;
#define inf 0x3f3f3f3f
int a[50];
int isTriangle(int x, int y, int z)
{
    if(x + y > z && x + z > y && z + y > x)return 1;
    else return 0;
}
int main()
{

    int n,ans;
    ans = inf;
    scanf("%d",&n);
    for(int i = 0; i < n; i ++)
    {
        scanf("%d", &a[i]);
    }
    for(int i = 0; i < n - 1; i ++)
    {
        for(int j = 0; j < n - i - 1; j ++)
            if(a[j] > a[j+1])
            {
                int temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
    }

    for(int i = 0; i < n; i ++)
    {
        for(int j = 0; j < n; j ++)
        {
            for(int k = 0; k < n; k ++)
            {
                if(isTriangle(a[i],a[j],a[k]) && i != j && j != k && i != k)
                {
                    if(ans > a[i]+a[j]+a[k])ans = a[i]+a[j]+a[k];
                }
            }
        }
    }
    if(ans == inf) printf("No\n");
    else
        printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mercury_Lc/article/details/107141189