熱門文章

2025年3月6日 星期四

ZeroJudge 解題筆記:f345. 新手練習題—陣列

作者:王一哲
日期:2025年3月6日



ZeroJudge 題目連結:f345. 新手練習題—陣列

解題想法


這題很簡單,有很多不同的寫法,我盡量列出已知的寫法。

Python 程式碼


寫法 1


先將資料轉成整數儲存到串列中,再用索引值反過來輸出,使用時間約為 1.6 s,記憶體約為 132.6 MB,通過測試。
n = int(input())  # 陣列長度
arr = list(map(int, input().split()))  # 陣列
for i in range(n-1, -1, -1):  # 用索引值反向輸出
    print(arr[i], end="\n" if i == 0 else " ")

寫法 2


直接用字串儲存到串列中,再用索引值反過來輸出,使用時間約為 1.4 s,記憶體約為 106.8 MB,通過測試。
n = int(input())  # 陣列長度
arr = list(input().split())  # 陣列
for i in range(n-1, -1, -1):  # 用索引值反向輸出
    print(arr[i], end="\n" if i == 0 else " ")

寫法 3


先將資料轉成整數儲存到串列中,用串列切片將串列反過來,最後用開箱運算子印出串列內容。使用時間約為 0.9 s,記憶體約為 140.3 MB,通過測試。
_ = int(input())  # 陣列長度,用不到
arr = list(map(int, input().split()))  # 陣列
arr = arr[::-1]  # 用串列切片將陣列反過來
print(*arr)  # 用開箱運算子印出陣列內容

寫法 4


可以將上方的程式碼第3、4行合併。使用時間約為 0.9 s,記憶體約為 147.9 MB,通過測試。
_ = int(input())  # 陣列長度,用不到
arr = list(map(int, input().split()))  # 陣列
print(*arr[::-1])  # 用串列切片將陣列反過來,再用開箱運算子印出陣列內容

寫法 5


將資料轉成整數存到串列中,用 reversed 將串列反過來,最後用開箱運算子印出串列內容。使用時間約為 0.9 s,記憶體約為 140.3 MB,通過測試。
_ = int(input())  # 陣列長度,用不到
print(*reversed(list(map(int, input().split()))))

寫法 6


將寫法 3 改用字串格式,使用時間約為 0.6 s,記憶體約為 109.3 MB,通過測試。
_ = int(input())  # 陣列長度,用不到
arr = list(input().split())  # 陣列
arr = arr[::-1]  # 用串列切片將陣列反過來
print(*arr)  # 用開箱運算子印出陣列內容

寫法 7


將寫法 4 改用字串格式,使用時間約為 0.6 s,記憶體約為 116.7 MB,通過測試。
_ = int(input())  # 陣列長度,用不到
arr = list(input().split())  # 陣列
print(*arr[::-1])  # 用串列切片將陣列反過來,再用開箱運算子印出陣列內容

寫法 8


將寫法 5 改用字串格式,使用時間約為 0.6 s,記憶體約為 109.1 MB,通過測試。
_ = int(input())  # 陣列長度,用不到
print(*reversed(list(input().split())))

寫法 9


如果使用字串格式,還可以用 join 將串列內容用空格連接成一個很長的字串一起輸出,使用時間約為 0.2 s,記憶體約為 120.5 MB,通過測試。
_ = int(input())  # 陣列長度,用不到
print(" ".join(reversed(list(input().split()))))


C++ 程式碼


寫法 1


先將資料轉成整數儲存到 array 中,再用索引值反過來輸出,使用時間約為 0.4 s,記憶體約為 4.1 MB,通過測試。
#include <iostream>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n; cin >> n;  // 陣列長度
    int arr[n];  // 陣列
    for(int i=0; i<n; i++) cin >> arr[i];
    for(int i=n-1; i>=0; i--) cout << arr[i] << " \n"[i == 0];  // 用索引值反向輸出
    return 0;
}

寫法 2


先將資料轉成整數儲存到 vector 中,再用索引值反過來輸出,使用時間約為 0.4 s,記憶體約為 4.1 MB,通過測試。
#include <iostream>
#include <vector>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n; cin >> n;  // 陣列長度
    vector<int> arr (n);  // 陣列
    for(int i=0; i<n; i++) cin >> arr[i];
    for(int i=n-1; i>=0; i--) cout << arr[i] << " \n"[i == 0];  // 用索引值反向輸出
    return 0;
}

寫法 3


先將資料轉成整數儲存到 vector 中,再用反向的迭代器輸出,使用時間約為 0.4 s,記憶體約為 4.1 MB,通過測試。
#include <iostream>
#include <vector>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n; cin >> n;  // 陣列長度
    vector<int> arr (n);  // 陣列
    for(int i=0; i<n; i++) cin >> arr[i];
    for(auto it = arr.crbegin(); it != arr.crend(); it++) cout << *it << " \n"[it == arr.crend()-1];  // 用反向的迭代器輸出
    return 0;
}

寫法 4


寫法 2 改用字串格式,使用時間約為 0.3 s,記憶體約為 30.9 MB,通過測試。
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n; cin >> n;  // 陣列長度
    vector<string> arr (n);  // 陣列
    for(int i=0; i<n; i++) cin >> arr[i];
    for(int i=n-1; i>=0; i--) cout << arr[i] << " \n"[i == 0];  // 用索引值反向輸出
    return 0;
}

寫法 5


寫法 3 改用字串格式,使用時間約為 0.3 s,記憶體約為 30.8 MB,通過測試。
#include <iostream>
#include <vector>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n; cin >> n;  // 陣列長度
    vector<int> arr (n);  // 陣列
    for(int i=0; i<n; i++) cin >> arr[i];
    for(auto it = arr.crbegin(); it != arr.crend(); it++) cout << *it << " \n"[it == arr.crend()-1];  // 用反向的迭代器輸出
    return 0;
}


沒有留言:

張貼留言