---
# System prepended metadata

title: 訓練場_4程式繳交處
tags: [訓練場_st]

---

---
title: 訓練場_4程式繳交處
tags: 訓練場_st
---
> [訓練場_4 題目](https://hackmd.io/@futurenest/code_training_4)

> [程式線上編譯環境](https://replit.com/)
:::warning
繳交規範（可以複製這裡的喔）

`### 自己名字`
`#### 題目一` 
```python
程式碼
```
`#### 題目二` 
```c++
程式碼
```
:::
### 示範 (1)
```python
def sequential_search(data, key):
  for i in range(len(data)):
    if key == data[i]:
      return i
  return -1

data = [40, 30, 20, 80, 60, 10, 50]
key = 60
print("原始資料：{}".format(data))
ret = sequential_search(data, key)
if ret == -1:
  print('找不到 {} QQ'.format(key))
else:
  print('找到 {} 索引值為：{}'.format(key, str(ret)))
```
```cpp
#include <iostream>

using namespace std;

/*
  學習：
  - 函式宣告
  - 將陣列用指標方式傳送
*/

void printArray(int*, int);
int sequentialSearch(int*, int, int);

int main() {
  int data [] = { 40, 30, 20, 80, 60, 10, 50 };
  int dataLength = (sizeof(data)/sizeof(*data));
  int key = 60;

  cout << "原始資料： ";
  printArray(data, dataLength);

  int ret = sequentialSearch(data, dataLength, key);
  if (ret == -1) cout << "找不到 " << key << " QQ" << endl;
  else cout << "找到 " << key << " 索引值為 " << ret << endl;
}

int sequentialSearch(int* data, int length, int key) {
  for (int i = 0; i < length; i++)
    if (key == data[i]) return i;
  return -1;
}

void printArray(int* data, int length) {
  cout << "[";
  for (int i = 0; i < length; i++) {
    cout << data[i];
    if (i != length-1) cout << ", ";
  }

  cout << "]" << endl;
}
```

### 示範 (2)
```python
def binary_search(data, key):
  low = 0
  high = len(data)-1
  while low <= high:
    mid = int((low + high) / 2)
    if key == data[mid]:
      return mid
    elif key > data[mid]:
      low = mid + 1
    else:
      high = mid - 1
  return -1

data = [40, 30, 20, 80, 60, 10, 50]
key = 60
data.sort()
print("排序後的資料：{}".format(data))
ret = binary_search(data, key)
if ret == -1:
  print('找不到 {} QQ'.format(key))
else:
  print('找到 {} 索引值為：{}'.format(key, str(ret)))
```

```cpp
#include <iostream>
#include <string>
#include <vector>

using namespace std;

/*************************************************
  學習：
  - array <=> vector
  - sorting operator funciton
*************************************************/

void printArray(int*, int);
int binarySearch(int*, int, int);

/*
  sorting operator funciton
  若 true: i 會放在 j 前面
*/
bool sortingASC (int i, int j) { return i<j; }
bool sortingDESC (int i, int j) { return i>j; }

int main() {
  int data [] = { 40, 30, 20, 80, 60, 10, 50 };
  int dataLength = (sizeof(data)/sizeof(*data));
  std::vector<int> dataVector (data, data + dataLength);

  int key = 60;

  cout << "排序後的資料：";
  std::sort (dataVector.begin(), dataVector.end(), sortingASC);  
  int* sortedData = &dataVector[0];
  printArray(sortedData, dataLength);

  int ret = binarySearch(sortedData, dataLength, key);
  if (ret == -1) cout << "找不到 " << key << " QQ" << endl;
  else cout << "找到 " << key << " 索引值為 " << ret << endl;
}

int binarySearch(int* data, int length, int key) {
  int low = 0;
  int high = length - 1;
  while (low <= high) {
    int mid = int((low + high) / 2);
    if (key == data[mid]) return mid;
    else if (key > data[mid]) low = mid + 1;
    else high = mid - 1;
  }

  return -1;
}

void printArray(int* data, int length) {
  cout << "[";
  for (int i = 0; i < length; i++) {
    cout << data[i];
    if (i != length-1) cout << ", ";
  }

  cout << "]" << endl;
}
```
