---
tags: DICE C
---
7-3 五數比大小
===
> [name=CHAWTeam] [time=Feb 17, 2022]
---
目錄:[DICE C語言程式破解](https://hackmd.io/@CHAWTeam/DiceC)
上一篇:[7-2 三數比大小](https://hackmd.io/@CHAWTeam/DiceC-7-2)
下一篇:[7-4 判斷閏年](https://hackmd.io/@CHAWTeam/DiceC-7-4)
---
題目
---
```!
寫一個程式,輸入5個整數,輸出最大值與最小值。
程式只須執行1次。
以下提供 2 種範例參考,你的輸出只會是其中一種:
輸入範例1:
5 4 3 2 1
輸出範例1:
最大值5
最小值1
輸入範例2:
6 8 5 3 9
輸出範例2:
最大值9
最小值3
```
程式碼
---
```c=
#include <stdio.h>
int main()
{
int a, b, c, d, e, t; //設「暫時 temporary 」定義為 t
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
if (a > b)
{
t = a;
a = b;
b = t;
}
if (b > c)
{
t = b;
b = c;
c = t;
}
if (c > d)
{
t = c;
c = d;
d = t;
}
if (d > e)
{
t = d;
d = e;
e = t;
}
printf("最大值%d\n", e);
if (a < b)
{
t = a;
a = b;
b = t;
}
if (b < c)
{
t = b;
b = c;
c = t;
}
if (c < d)
{
t = c;
c = d;
d = t;
}
if (d < e)
{
t = d;
d = e;
e = t;
}
printf("最小值%d\n", e);
return 0;
}
```
:::success
利用**氣泡排序法(Bubble Sort)** 的原理,持續將每個數字兩兩比較後,得出最大值與最小值。
> 推薦這篇寫得非常淺顯易懂的文章 **[[C++] 氣泡排序法(Bubble sort)](https://medium.com/@oturngo/study-note-01-%E6%B0%A3%E6%B3%A1%E6%8E%92%E5%BA%8F%E6%B3%95-bubble-sort-ee534b6f91eb)**
> [維基百科](https://zh.wikipedia.org/wiki/%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F)
:::
輸出
---
```!
5 4 3 2 1
最大值5
最小值1
```
---
[查看我們在HackMD上的所有筆記](https://hackmd.io/@CHAWTeam)
目錄:[DICE C語言程式破解](https://hackmd.io/@CHAWTeam/DiceC)
上一篇:[7-2 三數比大小](https://hackmd.io/@CHAWTeam/DiceC-7-2)
下一篇:[7-4 判斷閏年](https://hackmd.io/@CHAWTeam/DiceC-7-4)
---
{%hackmd Iiu5mOixR7yWkPHKCkabBg %}
<iframe class="LikeCoin" height="235" src="https://button.like.co/in/embed/chawteam/button?referrer=https://hackmd.io/@CHAWTeam/DiceC-7-3" width="100%"></iframe>
---
{%hackmd i1nMRrZcTFmTvoF897K9zg %}