如何根據火柴棒數量對字串進行排序?
引言
在本教程中,我們將實現一種基於表示數字或字元所需的火柴棒數量對字串進行排序的方法。在這個方法中,我們使用 N 根火柴棒對陣列進行排序。陣列可以包含數字、單詞或兩者兼而有之。火柴棒用於將它們排列成特定數字或字元的形狀。
演示1
Input = Arr = ["1", "3", "4"] Output = The sorted array is 1 4 3
解釋
在上面的輸入陣列中,陣列元素為 1、3 和 4
數字 1 需要 1 根火柴棒
數字 3 需要 5 根火柴棒
數字 4 需要 4 根火柴棒
演示2
Input = Arr = ["23", "HI", "ABC" ] Output = The sorted array is HI 23 ABC
解釋
在上面的輸入陣列中,陣列元素為 23、HI 和 C。
數字 23 需要 10 根火柴棒
字母 "HI" 需要 6 根火柴棒
字母 "ABC" 需要 17 根火柴棒
C++庫函式
sizeof() − 這是一個 C++ 中的編譯時一元運算子,用於查詢變數、常量和資料型別的尺寸。
sizeof(value);
vector − 它是一個 C++ 中的動態陣列。它提供有效的陣列功能。它是用於儲存元素的資料結構之一。
vector<data_type> vcetor_name;
vector:begin() − 它是 vector 類中的一個預定義函式,定義在 <vector> 標頭檔案中。它返回 vector 的起始元素的指標。
vector_name.begin();
vector:end() − 它是 vector 類中的一個預定義函式,定義在 <vector> 標頭檔案中。它返回 vector 的最後一個元素的指標。
vector_name.end();
vector:sort() − 它是 vector 類中的一個預定義函式,定義在 <vector> 標頭檔案中。它使用 begin() 和 end() 函式作為引數對 vector 元素進行排序。
sort(vector_name.begin(), vector_name.end());
unordered_map − 它是 C++ 中的一種資料結構,用於儲存具有唯一鍵值對的元素。其元素不會以特定方式排序。
unordered_map<data_type> map_name;
size() − 它是 C++ 中的一個庫函式,返回輸入值的長度。它定義在 C++ 的標準庫中。
value.size();
auto − 它是 C++ 中的自動變數。它有助於在執行時宣告變數的資料型別。
auto auto_name;
演算法
獲取輸入陣列。
宣告一個變數來儲存製作 26 個字母中的每一個所需的火柴棒數量。
宣告一個變數來儲存製作 0 到 9 的數字所需的火柴棒數量。
迭代每個陣列元素以查詢所需的火柴棒數量。
根據火柴棒數量對陣列元素進行排序。
列印排序後的陣列元素。
示例 1
我們使用 C++實現了教程問題陳述。使用 vector 來儲存陣列字串。
#include <bits/stdc++.h>
using namespace std;
// array to store the number of matchsticks required to form characters from A to Z
int matchsticksAlphabets[] = { 6, 7, 4, 6, 5, 4, 6, 5, 2, 4, 4, 3, 6, 6, 6, 5, 7, 6, 5, 3, 5, 4, 6, 4, 3, 4 };
//array to store the number of matchsticks required to from numbers from 0 to 9
int matchsticksNumber[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
// Function for counting the number of matchsticks using count variable
int countingSticks(string strVar) {
int cntVar = 0;
// iterating each string of the array through for loop
for (int x = 0; strVar[x]; x++) {
char charVar = strVar[x];
if (charVar >= 'A' && charVar <= 'Z')
cntVar += matchsticksAlphabets[charVar - 'A'];
else
cntVar += matchsticksNumber[charVar - '0'];
}
return cntVar;
}
// Function for sorting the array elements as per number of matchsticks
void sortingArr(string arrEle[], int s){
vector<pair<int, string> > vpVar; // Vector for storing the number of matchsticks for each string
for (int x = 0; x < s; x++) {
vpVar.push_back(make_pair(countingSticks(arrEle[x]),arrEle[x]));
}
sort(vpVar.begin(), vpVar.end()); // function for sorting the vector elements
cout << "The sorted array is: ";
for (int x = 0; x < vpVar.size(); x++) // Printing the vector with sorted elements
cout << vpVar[x].second << " ";
}
int main(){
string arrEle[] = { "23", "HI", "ABC" };
int s = sizeof(arrEle) / sizeof(arrEle[0]);
sortingArr(arrEle, s); //calling function for sorting the array
return 0;
}
輸出
The sorted array is: HI 23 ABC
示例 2
我們使用 C++實現了本教程中基於表示其字元所需的火柴棒數量對字串進行排序的問題。為了實現這種方法,我們使用 map 及其函式根據火柴棒的數量對字串進行排序。比較運算子用於對陣列元素進行排序。
#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_map>
using namespace std;
//array for storing the number of matchsticks to form alphabets from A to Z
int alphabetSticks[] = { 6, 7, 4, 6, 5, 4, 6, 5, 2, 4, 4, 3, 6, 6, 6, 5, 7, 6, 5, 3, 5, 4, 6, 4, 3, 4 };
// array for storing the number of matchsticks to form numbers from 0 to 9
int numberSticks[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
// declaring function for count the matchsticks
int countingSticks(const string& strVar, const unordered_map<char, int>& matchstickCounts) {
int cntVar = 0;
for (char charVar : strVar) {
auto itVar = matchstickCounts.find(charVar); //declaring automatic variable
if (itVar != matchstickCounts.end())
cntVar += itVar->second;
}
return cntVar;
}
//function for finding the more matchsticks using map functions
bool compareStringsByMatchsticks(const string& strVar1, const string& strVar2, const unordered_map<char, int>& matchstickCounts) {
int matchsticks1 = countingSticks(strVar1, matchstickCounts);
int matchsticks2 = countingSticks(strVar2, matchstickCounts);
return matchsticks1 < matchsticks2;
}
// declaring a function for sorting the array with number of matchsticks
void sortedArr(string arrVar[], int s, const unordered_map<char, int>& matchstickCounts) {
sort(arrVar, arrVar + s, [&](const string& strVar1, const string& strVar2){
return compareStringsByMatchsticks(strVar1, strVar2, matchstickCounts);
});
cout << "The sorted array elements are : ";
for (int x = 0; x < s; x++) // Printing the array sorted on the basis of the number of matchsticks
cout << arrVar[x] << " ";
}
int main() {
unordered_map<char, int> matchstickCounts; // Define map for storing the strings
for (int x = 0; x < 26; x++)
matchstickCounts['A' + x] = alphabetSticks[x];
for (int x = 0; x < 10; x++)
matchstickCounts['0' + x] = numberSticks[x];
string arrVar[] = { "123", "HI", "ABC" };
int s = sizeof(arrVar) / sizeof(arrVar[0]);
sortedArr(arrVar, s, matchstickCounts);
return 0;
}
輸出
The sorted array elements are : HI 123 ABC
結論
我們已經完成了本教程。在本教程中,我們根據火柴棒的數量對陣列字串進行了排序。火柴棒構成字元和數字。所需的火柴棒總數儲存在兩個陣列中。我們使用 map 和 vector 實現了一種解決該任務的方法。map 和 vector 的不同函式用於透過查詢陣列中每個字串的火柴棒數量來對陣列元素進行排序。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP