最大和最小的四个整数(不阵列,无功能,最少的“如果”语句) [英] Biggest and smallest of four integers (No arrays, no functions, fewest 'if' statements)

查看:113
本文介绍了最大和最小的四个整数(不阵列,无功能,最少的“如果”语句)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您看,我自学自己的C ++(不完全,我还在拖延-_-)。所以,现在我开始大学,他们教C和他们让我们做输入四个整数的计划,我们必须告诉最大和最小的了出来。很简单,不是吗?

You see, I've self-taught myself C++ (not completely, I'm still procrastinating -_-). So, now I started university and they're teaching C and they made us do a program of inputting four integers and we have to tell the largest and smallest out of them. Simple, no?

问题是,我已经有功能和阵列很好的理解。是的,我可以在阵列中,没有任何问题设定此。但由于这是第一次实验中,我们还没有学习了,所以我不能使用任何这些,这会是非常简单的这一点。

The thing is, I already have good understanding of functions and arrays. Yes, I CAN program this in arrays, no problem. But since this was the first lab, we haven't 'learned' that yet, so I can't use any of those, it'd be very simple with that.

这是我写的有(感到不对劲不知)。

This is what I wrote there (it feels wrong somehow).

#include<stdio.h>

int main(void)
{
    int first, second, third, fourth;
    printf("Enter four integers (separated by space): ");
    scanf("%d %d %d %d", &first, &second, &third, &fourth);

    if((first>second) && (first>third) && (first>fourth))
        printf("\nFirst number is largest");
    else if((second>first) && (second>third) && (second>fourth))
        printf("\nSecond number is largest");
    else if((third>second) && (third>first) && (third>fourth))
        printf("\nThird number is largest");
    else if((fourth>second) && (fourth>third) && (fourth>first))
        printf("\nFourth number is largest");

    if((first<second) && (first<third) && (first<fourth))
        printf("\nFirst number is smallest");
    else if((second<first) && (second<third) && (second<fourth))
        printf("\nSecond number is smallest");
    else if((third<second) && (third<first) && (third<fourth))
        printf("\nThird number is smallest");
    else if((fourth<second) && (fourth<third) && (fourth<first))
        printf("\nFourth number is smallest");

    printf("\n"); 
    return 0;
}

正如你所看到的,它太长而枯燥的和复杂的。但看到所有我们已经覆盖类现在的循环的决定语句。是否有这样做的更优雅的方式? 其中一个使用较少的如果取值?这并不是说有什么错,但它可能是更好的。

As you can see, it's too long and boring and complex. But seeing that all we've covered in class for now are loops and decision statements. Is there a more elegant way of doing this? One which uses fewer ifs? Not that there is something wrong with this, but it could be better.

P.S。这不正是'功课'或任何东西。我做了一个节目,我只是想知道什么我可以做,使之更好,学习更好的编程实践。

P.S. This isn't exactly 'homework' or anything. I made a program, I just wanted to know what I could have done to make it better and to learn better programming practices.

推荐答案

根据业务方案的条件。

但看到所有我们已经覆盖类现在的循环的决定语句。是否有这样做的更优雅的方式? 其中一个使用较少的如果取值

But seeing that all we've covered in class for now are loops and decision statements. Is there a more elegant way of doing this? One which uses fewer ifs?

只有一个如果和一个否则,如果语句和一个循环可以做这个任务。 简单和短!

Only one if and one else if statement and one for loop can do this task. Simple and short!

#include <stdio.h>

int main()
{
    int num, max, min;

    printf ("Enter four numbers: ");
    scanf ("%d", &num);
    max = min = num;

    for (int i = 0; i < 3; i++)
    { 
        scanf ("%d", &num);
        if (max < num)
            max = num;
        else if (min > num)
            min = num;
    }

    printf ("The smallest and largest of given four numbers are %d and %d respectively.\n", min,  max);
    return 0;
}

这篇关于最大和最小的四个整数(不阵列,无功能,最少的“如果”语句)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆