---
# System prepended metadata

title: 程式設計c++(3)

---

# 程式設計c++(3)

## 比較三數大小，求最大
### 方法1:使用 if 語句
 說明:
 使用方法1較費時，因為每項程式都要跑一遍，不管答案是否已出現。
```cpp=
#include <iostream>
using namespace std;

int main() {    
    float n1, n2, n3;

    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;

    if(n1 >= n2 && n1 >= n3)
        cout << "Largest number: " << n1;

    if(n2 >= n1 && n2 >= n3)
        cout << "Largest number: " << n2;
    
    if(n3 >= n1 && n3 >= n2)
        cout << "Largest number: " << n3;
  
    return 0;
}
```
#### 流程圖:
![](https://i.imgur.com/gALSOIt.jpg)

### 方法2:使用 if...else
說明:
第10行程式中
" (X)&&(Y) "意思是指過程中(X),(Y)兩項條件都成立。
但如果" (X)||(Y) "是指有其中一個條件成立。

```cpp=
#include <iostream>
using namespace std;

int main() {
    float n1, n2, n3;

    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;

    if((n1 >= n2) && (n1 >= n3))
        cout << "Largest number: " << n1;
    else if ((n2 >= n1) && (n2 >= n3))
        cout << "Largest number: " << n2;
    else
        cout << "Largest number: " << n3;
    
    return 0;
}
```
#### 流程圖:
![](https://i.imgur.com/HVpQHlj.jpg)

### 方法3:使用嵌套 if...else 
```cpp=
#include <iostream>
using namespace std;

int main() {
    float n1, n2, n3;

    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;

    if (n1 >= n2) {
        if (n1 >= n3)
            cout << "Largest number: " << n1;
        else
            cout << "Largest number: " << n3;
    }
    else {
        if (n2 >= n3)
            cout << "Largest number: " << n2;
        else
            cout << "Largest number: " << n3;
    }

    return 0;
}
```
#### 流程圖:
![](https://i.imgur.com/QK6MlVw.jpg)


## 閏年計算
### 方法1:使用 if...else
```cpp=
#include <iostream>
using namespace std;

int main() {

  int year;
  cout << "Enter a year: ";
  cin >> year;

  if (year % 400 == 0) {
    cout << year << " is a leap year.";
  }
  
  else if (year % 100 == 0) {
    cout << year << " is not a leap year.";
  }

  else if (year % 4 == 0) {
    cout << year << " is a leap year.";
  }
  else {
    cout << year << " is not a leap year.";
  }

  return 0;
}
```
#### 流程圖:
![](https://i.imgur.com/0m3pKAI.jpg)

### 方法2:使用嵌套 if 檢查閏年
```cpp=
#include <iostream>
using namespace std;

int main() {

  int year;

  cout << "Enter a year: ";
  cin >> year;

  if (year % 4 == 0) {
    if (year % 100 == 0) {
      if (year % 400 == 0) {
        cout << year << " is a leap year.";
      }
      else {
        cout << year << " is not a leap year.";
      }
    }
    else {
      cout << year << " is a leap year.";
    }
  }
  else {
    cout << year << " is not a leap year.";
  }

  return 0;
}

```
#### 流程圖:
![](https://i.imgur.com/K4UoNID.jpg)

### 方法3:使用邏輯運算符
說明:
第11行程式中
((N && Y)|| C )意思是
"N成立且Y成立"這項條件成立 或 "C"成立 即為Yes

```cpp=
#include <iostream>
using namespace std;

int main() {

  int year;

  cout << "Enter a year: ";
  cin >> year;

  if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
    cout << year << " is a leap year.";
  }
  else {
    cout << year << " is not a leap year.";
  }

  return 0;
}

```
#### 流程圖:
![](https://i.imgur.com/FEZuxW6.jpg)
