簡體   English   中英

如何確定3個整數的最大和最小

[英]How to determine the largest and smallest of 3 integers

我正在上大學,必須確定三個輸入數字中最大,最小和中間一個。 但是沒有一些值(例如2 5 4),我真的不知道我正在經歷什么。

我想我在其他情況下缺少某些條件。

#include<stdio.h>

int main()
{
    puts("Enter three numbers separated by a space to determine what is the greatest, what is the one in the middle and what is the lowest.\n");

    unsigned int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    char z[] = "The three numbers are equal.\n";
    char x1[] = "is greater than the two equal numbers you entered."; char x2[] = "Is less than the two equal numbers you entered.";
    char y1[] = "is the largest,"; char y2[] = "is in the middle"; char y3[] = "is the lowest.";

    if (a==b && b==c && c==a) printf("%s\n",z);
    else if (a>b && b==c) printf("%d %s (%d)\n",a,x1,b); else if (a<b && b==c) printf("%d %s (%d)\n",a,x2,b);
    else if (a>c && c==b) printf("%d %s (%d)\n",a,x1,c); else if (a<c && c==b) printf("%d %s (%d)\n",a,x2,c);
    else if (b>a && a==c) printf("%d %s (%d)\n",b,x1,a); else if (b<a && a==c) printf("%d %s (%d)\n",b,x2,a);
    else if (b>c && c==a) printf("%d %s (%d)\n",b,x1,c); else if (b<c && c==a) printf("%d %s (%d)\n",b,x2,c);
    else if (c>a && a==b) printf("%d %s (%d)\n",c,x1,a); else if (c<a && a==b) printf("%d %s (%d)\n",c,x2,a);
    else if (c>b && b==a) printf("%d %s (%d)\n",c,x1,b); else if (c<b && b==a) printf("%d %s (%d)\n",c,x2,b);

    else if (a>b>c) printf("%d %s %d %s %d %s\n",a,y1,b,y2,c,y3);
    else if (a<b<c) printf("%d %s %d %s %d %s\n",c,y1,b,y2,a,y3);
    else if (b>a>c) printf("%d %s %d %s %d %s\n",b,y1,a,y2,c,y3);
    else if (b<a<c) printf("%d %s %d %s %d %s\n",c,y1,a,y2,b,y3);
    else if (c>a>b) printf("%d %s %d %s %d %s\n",c,y1,a,y1,b,y3);
    else if (c>b>a) printf("%d %s %d %s %d %s\n",c,y1,b,y2,c,y3);
    else if (a<b>c && c<a) printf("%d %s %d %s %d %s\n",b,y1,a,y2,c,y3); // b a c
    else if (a<b>c && a<c && c<b) printf("%d %s %d %s %d %s\n",b,y1,c,y2,a,y3); // b c a
    else if (a<c>b && b<a) printf("%d %s %d %s %d %s\n",c,y1,a,y2,b,y3); // c a b
    else if (a<c>b && a<b) printf("%d %s %d %s %d %s\n",c,y1,b,y2,a,y3); // c b a
    else if (b<a>c && c<b) printf("%d %s %d %s %d %s\n",a,y1,b,y2,c,y3); // a b c
    else if (b<a>c && b<c) printf("%d %s %d %s %d %s\n",a,y1,c,y2,b,y3); // a c b



    else {
        printf("There's no valid data");}
}

我還想提出一些建議來優化代碼。 這個想法是盡可能優化的。

if (a>b>c)

這並沒有按照您的想法做。 C不支持“鏈接條件”(很少有通用語言支持)。 您需要顯式檢查a > b && b > c 否則,您將比較a與b,如果生成的結果為“真”(C中為1),則將比較1與c,否則將比較0與c。 當然,這不是您想要的。

編輯:至於優化,算了。 首先專注於使程序清晰簡潔。 因此,您知道,沒有任何普通程序像現在這樣擁有如此出色的if / else子句。 我首先要解決這個問題,因為那就像拇指酸痛一樣突出。

您的代碼有問題的地方是C ++不能做類似a < b < c事情。 這意味着(a < b) < c ,將是1 < c0 < c 您必須執行a < b && b < c

另外,您在那里做的工作太多了。 我認為我從未見過這么多else if

您可以定義兩個函數,這些函數將返回兩個數字的較大和較小值:

int imax(int a, int b) {
    return a < b ? b : a;
}

int imin(int a, int b) {
    return a < b ? a : b;
}

然后在所有三個上使用它來獲取高,中和最低數字。

int high = imax(imax(a, b), c);
int mid  = imax(imin(imax(a, b), c), imin(a, imax(b, c)));
int low  = imin(imin(a, b), c);

然后進行兩次檢查,一次檢查是否相等,如果兩次相等,則進行一次檢查。 如果這兩個檢查都不成立,那么它們都不相等,您可以給出低,高和中的數字,如下所示:

if (high == mid && mid == low)
    // all numbers are the same
else if (high == mid || high == low || mid == low)
    // two of the numbers are the same
else
    // the numbers are not the same, output high, mid, and low

您可以使用嵌套的ifs編寫更簡單的代碼。 我建議開始:

if (a > b) {
   if (b > c) { 
     ...

另外,您知道任何數字是否可以相等嗎? 那有很大的不同。

一種優化建議是在決定輸出什么之前對數字進行排序。 那么您只需要處理四種情況(全部相等,一個更大,一個較小,所有不相等)。

另外,我相信在C中,如果您編寫a < b < c ,它將首先將a < b評估為0或1,然后評估1 < c0 < c

如果您確定哪個最大,那么您對其他2個會有什么了解? 分而治之。

為了以不同的方式回答您的問題,以下是如何在外殼上的數據文件中找到最小和最大數字的方法:

$ echo 2 12 8 > data.txt
$ cat data.txt | tr ' ' '\n' | sort -n | head -n 1
2
$ cat data.txt | tr ' ' '\n' | sort -n | tail -n 1
12

首先, cat將列出的文件內容串聯在一起,這里只是data.txt (例如,它讀取文件)。 接下來, tr將空格字符' '轉換為換行符'\\n' 然后, sort每行進行數字sort -n 最后, head打印多行,此處為1(例如,最低編號),而tail從頭開始打印多行,僅從末端(例如最高編號)打印1。

由於您需要的是三個數字中的最小和最大,因此您可以創建兩個函數,一個返回最小的值,另一個返回最大的值。 然后您打印結果。

注意:最好對代碼進行分隔。 具有小的功能,一次只能完成一項小任務。 您會發現以這種方式創建代碼更容易,其他人將在不得不閱讀您的程序時欣賞它們。

#include <stdio.h>

int smallestVal(int val1, int val2, int val3);
int largestVal(int val1, int val2, int val3);

int main()
{
 //Assuming these are your numbers:
 int first=7;
 int second=4;
 int third=5;

 int smallest=0;  //These are what you aiming for
 int largest=0;

 smallest = smallestVal(first, second, third);
 largest = largestVal(first, second, third);

 printf("smallest=%d  largest=%d\n", smallest, largest);
 return 0;
}

int smallestVal(int val1, int val2, int val3)
{
 int smallest = val1;

 if (val2 <= smallest)
 smallest = val2;

 if(val3 <= smallest)
 smallest = val3;

 //continue adding more if... you need so.
 //but remember to add more arguments to your function.

 return smallest;
}

int largestVal(int val1, int val2, int val3)
{
    int largest = val1;

    if(val2 >=largest)
    largest = val2;

    if(val3 >= largest)
    largest = val3;

    //continue adding more if... you need so.
    //but remember to add more arguments to your function.

    return largest;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM