[C] 排序取最大最小

某天某月看到某題目,好久沒發程式語言的文章了就來發一篇救救同學吧。其實這個題目應該是很簡單的,看到題目第一秒就想到直接氣泡排序法(因為我懶惰,用比較簡單的演算法)然後取頭尾就搞定了,結果很不幸的寫完之後想到了一件事...

恩,就是,老師根本還沒教到這邊啊!!!才剛提到「for 迴圈」而已,要怎麼跟同學解釋「陣列」等等概念呢 =   =囧

題目:寫一個程式向使用者要求輸入五個整數。該程式將輸出其最大最小值。


以氣泡排序法從小到大排列,取頭尾(最小最大)值:
(感謝學長與網友糾正我錯誤的寫法,已修正。)
#include <stdio.h>

int main() {
 while(1){
  printf("Please enter five integers: ");
  int a[5], i;
  for(i = 0; i < 5; i++){
   scanf("%d", &a[i]);
  }
  
     int j = 0, temp = 0;
     for( i = 0; i < 5; i++) {
         for( j = 0; j < 4 - i; j++) {
             if( a[j] < a[j+1] ) {
                 temp = a[j];
                 a[j] = a[j+1];
                 a[j+1] = temp;
             }
         }
     }
  printf("The largest is %d\nThe smallest is %d\n", a[4], a[0]);
 }
}

然後後來想了半天用單純全部 if if if... 搞出來的,數列逐次比對,從大排到最小取頭尾(最大最小)值:
#include <stdio.h>

int main(int argc, char *argv[])
{
    int a,b,c,d,e;
    int temp;
    scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
    //大到小
    if(a>b){
         temp = b;
         b = a;
         a = temp;
    }
    if(a>c){
         temp = c;
         c = a;
         a = temp;
    }
    if(a>d){
         temp = d;
         d = a;
         a = temp;
    }
    if(a>e){
         temp = e;
         e = a;
         a = temp;
    }
    //
    if(b>c){
         temp = c;
         c = b;
         b = temp;
    }
    if(b>d){
         temp = d;
         d = b;
         b = temp;
    }
    if(b>e){
         temp = e;
         e = b;
         b = temp;
    }
    if(b>a){
         temp = a;
         a = b;
         b = temp;
    }
    //
    if(c>d){
         temp = d;
         d = c;
         c = temp;
    }
    if(c>e){
         temp = e;
         e = c;
         c = temp;
    }
    if(c>a){
         temp = a;
         a = c;
         c = temp;
    }
    if(c>b){
         temp = b;
         b = c;
         c = temp;
    }
    //
    if(d>e){
         temp = e;
         e = d;
         d = temp;
    }
    if(d>a){
         temp = a;
         a = d;
         d = temp;
    }
    if(d>b){
         temp = b;
         b = d;
         d = temp;
    }
    if(d>c){
         temp = c;
         c = d;
         d = temp;
    }
    //
    if(e>a){
         temp = a;
         a = e;
         e = temp;
    }
    if(e>b){
         temp = b;
         b = e;
         e = temp;
    }
    if(e>c){
         temp = c;
         c = e;
         e = temp;
    }
    if(e>d){
         temp = d;
         d = e;
         e = temp;
    }
    printf("The largest is %d\n",a);
    printf("The smallest is %d\n",e);
}
原來一開始寫得有小問題,結果後來看一看反而比較像選擇排序法的樣子...
總之想打這篇只是因為那一堆 if 實在很累人,還好題目只有五個數...

0925 新增參考答案,其實這題最簡單的寫法就是直接取大小...
#include <stdio.h>

main()
{
 int a1, a2, a3, a4, a5;
 int min, max;
 
 while(1){ 
  printf("Please enter five integers: ");
  scanf("%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5);
  
  min = a1;
  max = a1;
  
  if(a2 > max) max = a2;
  if(a3 > max) max = a3;
  if(a4 > max) max = a4;
  if(a5 > max) max = a5;
  
  if(a2 < min) min = a2;
  if(a3 < min) min = a3;
  if(a4 < min) min = a4;
  if(a5 < min) min = a5;
  
  printf("The largest is %d\n", max);
  printf("The smallest is %d\n\n", min);
 }
}

留言

  1. 這個題目因為輸出僅兩個數字的關係,
    所以只需要用兩個變數去記最大跟最小。
    第一次輸入的數字 a1 = max = min
    接著每次輸入完就比對一次,最後在印出 max min 就好

    回覆刪除

張貼留言

本月最夯

楓之谷洋蔥4.1.2 - 最後更新日期04/03

偷用電腦,怎知?事件檢視器全記錄!(開機時間、啟動項時間...)