陣列是專門用於在一系列記憶體區域中保留同類型資料的資料結構。使用陣列的主要好處是我們可以使用索引參數從任何位置存取它們。然而,插入和刪除資料需要順序操作,這將使這個資料結構變成線性資料結構。我們可以簡單地使用方括號中的索引或位置號碼來從陣列中提取元素。本文將示範如何在C 中從陣列中讀取最近的k個數字。
Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69] We have another number k = 4 The number of elements in A is 9 The output will be the last k elements from A, which are: 12, 35, 74, 69
We have the elements inside the array for every array, and the quantity n is also crucial. The number n indicates how many valid elements are there in an array. The size of the array might not match the n. Alarraough an can have a maximum of Z elements, only n of them must be valid; the remaining slots are empty. In this case, k must be less than or equal to n in order to retrieve the kth element from the less than or equal to n in order to retrieve the kth element from the array. Before ta array. B components, we must inspect it. For a better understanding, let's have a look at the algorithm.
讀取一個陣列 A 作為輸入。同時接受元素的數量:n 和 k,以讀取 A 中的前 k 個元素
建立一個空數組 B
如果 k < n,則
for i in range 0 to k - 1, do
B[ i ] = A[ n - k i ]
#end for
end if
#返回 B
#include <iostream> # define Z 50 using namespace std; void displayArr(int arr[], int n){ for( int i = 0; i < n; i++ ){ cout << arr[ i ] << ", "; } cout << endl; } void pickLastKElement( int A[], int n, int B[], int &m, int k) { if( k <= n ){ for( int i = 0; i < k; i++ ) { B[ i ] = A[ n - k + i ]; m = m + 1; } } } int main() { int A[ Z ] = {57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14}; int n = 12; int B[ Z ]; int m = 0; cout << "Given Array: "; displayArr( A, n ); pickLastKElement( A, n, B, m, 7 ); cout << "The last 7 element from A: "; displayArr( B, m ); m = 0; pickLastKElement( A, n, B, m, 10 ); cout << "The last 10 element from A: "; displayArr( B, m ); }
Given Array: 57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14, The last 7 element from A: 52, 86, 14, 76, 65, 32, 14, The last 10 element from A: 44, 19, 86, 52, 86, 14, 76, 65, 32, 14,
在上述方法中,靜態陣列用於儲存和檢索陣列元素。使用向量也可以實現相同的功能。向量是C STL的一部分,它是動態數組。讓我們來看看程式碼。算法保持不變。
#include <iostream> #include <vector> # define Z 50 using namespace std; void displayArr( vector<int> v ){ for( int i = 0; i < v.size() ; i++ ){ cout << v[ i ] << ", "; } cout << endl; } vector<int> pickLastKElement( vector<int> A, int k) { vector<int> B; if( k <= A.size() ){ for( int i = 0; i < k; i++ ) { B.push_back( A[ A.size() - k + i ] ); } } return B; } int main() { vector<int> A = {57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14}; vector<int> B; cout << "Given Array: "; displayArr( A ); B = pickLastKElement( A, 7 ); cout << "The last 7 element from A: "; displayArr( B ); B = pickLastKElement( A, 10 ); cout << "The last 10 element from A: "; displayArr( B ); }
Given Array: 57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14, The last 7 element from A: 52, 86, 14, 76, 65, 32, 14, The last 10 element from A: 44, 19, 86, 52, 86, 14, 76, 65, 32, 14,
最後一種方法是手動建立一個空向量,然後逐一複製元素。然而,我們可以直接使用向量迭代器在向量建構函數中複製最後k個元素。讓我們看一下程式碼來理解這個概念。
#include <iostream> #include <vector> # define Z 50 using namespace std; void displayArr( vector<int> v ){ for( int i = 0; i < v.size() ; i++ ){ cout << v[ i ] << ", "; } cout << endl; } vector<int> pickLastKElement( vector<int> A, int k) { vector<int> B( A.begin() + (A.size() - k), A.end() ); return B; } int main() { vector<int> A = {57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14}; vector<int> B; cout << "Given Array: "; displayArr( A ); B = pickLastKElement( A, 7 ); cout << "The last 7 element from A: "; displayArr( B ); B = pickLastKElement( A, 10 ); cout << "The last 10 element from A: "; displayArr( B ); }
Given Array: 57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14, The last 7 element from A: 52, 86, 14, 76, 65, 32, 14, The last 10 element from A: 44, 19, 86, 52, 86, 14, 76, 65, 32, 14,
在這裡,使用A向量的最後k個元素創建了B向量。使用begin() 方法取得第一個項目的位址,並使用偏移量begin() (A.size() − k)作為結束點,這樣就可以指向最後k個元素。
本文介紹了從給定數組中讀取或選擇最後n個數字的三種不同方法。第二種和第三種解決方案是基於向量,而不是第一種方法使用的靜態預設數組。前兩個問題的答案很簡單。我們使用for迴圈逐一複製最後k個元素。最後一種技術是最簡單的,它使用向量構造函數透過使用另一個向量的迭代器來複製元件來產生一個向量。
以上是取得數組中最後給定數量的項目的C++程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!