要求三角形的最长边c

时间:2017-04-11 01:38:01

标签: c geometry

我试图让我的程序打印出三角形中最长的一面,但我似乎得到了其他的,如果陈述不正确我怎么能解决这些问题以及我做错了什么。

            if (a >= b && a >= c)
    printf("A is the largest side.", a);

if (b >= a && b >= c)
    printf("B is the largest side.", b);

if (c >= a && c >= b)
    printf("C is the largest side.", c);

else if (a <= b && a <= c)
    printf("A is the smallest side.", a);

else if (b <= a && b <= c)
    printf("B is the smallest side.", b);

else if (c <= a && c <= b)
    printf("C is the smallest side.", c);

3 个答案:

答案 0 :(得分:0)

也许这会比你的if-sequence

更好 为了你的目的,

采用它

int main(){
    char longest_side;
    int a, b, c;
    longest_side = (a > b) ? ((a > c) ? ('a') : ('c')) : ((b > c) ? ('b') : ('c'));
    retrun 0;
}

如果您想保留自己的代码,请将其更改为

if (a > b && a > c)
    printf("A is the largest side.", a);    
else if (b > a && b > c)
    printf("B is the largest side.", b);    
else
    printf("C is the largest side.", c);

if (a < b && a < c)
    printf("A is the smallest side.", a);    
else if (b < a && b < c)
    printf("B is the smallest side.", b);
else 
    printf("C is the smallest side.", c);

另请注意您的printf call doesn't output your side length,因为您无法使用"%d"

printf("C is the smallest side. Length: %d", c);

答案 1 :(得分:0)

找到最大数量的简单方法恕我直言:

#include <stdio.h>

int max(int x, int y)
{
    if (x > y) return x;
    else return y;
}

int main(void)
{
    int a, b, c;
    a = 10; b = 20; c = 30;

    printf("%d\n", max(max(a, b), c));
}

我相信你可以从这里接受。

答案 2 :(得分:-1)

我想你可以尝试一下。在这里你可以得到一些漂亮的解决方案,用if-else和函数

找到max和min
#include <stdio.h>
#include <math.h>

int max(int a, int b)
{
    return 0.5 * (a + b + fabs(a - b));
}

int min(int a, int b)
{
    return 0.5 * (a + b - fabs(a - b));
}

int main(void)
{
    int a, b, c;

    scanf("%d %d %d", &a, &b, &c);

    if(a == max(max(a, b), c))
        printf("A is the largest side. And it\'s value is: %d\n", a);
    else if(b == max(max(a, b), c))
        printf("B is the largest side. And it\'s value is: %d\n", b);
    else
        printf("C is the largest side. And it\'s value is: %d\n", c);

    if(a == min(min(a, b), c))
        printf("A is the smallest side. And it\'s value is: %d\n", a);
    else if(b == min(min(a, b), c))
        printf("B is the smallest side. And it\'s value is: %d\n", b);
    else
        printf("C is the smallest side. And it\'s value is: %d\n", c);

    return 0;
}